Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


SQL Puzzle | The 3 Decimal Digits Puzzle

In this puzzle you have find out the rows where the value of column contains a digit and 3 decimal places after that. Please let me know if you know any easy method than i have mentioned. Please check out the sample input values and sample expected output below.

Sample Input

Val
1.0
1.234
0.34
34.000
34
23.456
1.1
12.

Expected Output

Val
1.234
34.000
23.456

Script

Use below script to create table and insert sample data into it.

--

CREATE TABLE [3Decimal]
(
	Val VARCHAR(100)
)
GO

INSERT INTO [3Decimal] VALUES
('1.0'),('1.234'),('0.34'),('34.000'),('34'),('23.456'),('1.1'),('12.')


--

Rules/Restrictions

The solution should be should use “SELECT” statement or “CTE”.

Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com

Solution – 1


--


SELECT * FROM [3Decimal]
WHERE Val LIKE '%.___'
	
--

Solution – 2


--

SELECT * FROM [3Decimal]
WHERE RIGHT(Val,4) LIKE '.%'
	
--

Output

--

/*------------------------

SELECT * FROM [3Decimal]
WHERE Val LIKE '%.___'
------------------------*/
Val
----------------------------------------------------------------------------------------------------
1.234
34.000
23.456

(3 row(s) affected)

--

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com

Advertisements