SQL Puzzle | Remove trailing zeros puzzle – The DOUBLE PRECISION/FLOAT(53) data type | Advanced SQL

In this puzzle you have to create a new columns from the vals column. In that column you need to remove the training zeros. So if a value is 13.90 then it should be 13.9 in the final output. For more details please refer the sample input data and the expected output.

Have you ever heard of DOUBLE PRECISION data type?

Well It is a floating point data type. Basically double precision is equivalent to float(53). I shall write one more post on Float[{n}] to explain this in detail.

Sample Input

Id Vals
1 10.000
1 10.000
1 10.300
1 10.900
1 10.897
1 10.017
1 0.000

Expected Output

Id Vals NewVals
1 10.000 10
1 10.000 10
1 10.300 10.3
1 10.900 10.9
1 10.897 10.897
1 10.017 10.017
1 0.000 0

Script – DDL and INSERT Sample Data

 ```-- CREATE TABLE RemoveTrailingZeros ( Id SmallINT ,Vals DECIMAL(10,3) ) GO INSERT INTO RemoveTrailingZeros VALUES (1,10), (1,10.00), (1,10.3), (1,10.9000), (1,10.897), (1,10.01700), (1,0.000) GO SELECT * FROM RemoveTrailingZeros GO -- ```

SOLUTION 1

 ```-- SELECT * , CAST(Vals AS FLOAT) NewVals FROM RemoveTrailingZeros -- ```

OUTPUT – 1

 ```-- Id Vals NewVals ------ --------------------------------------- ---------------------- 1 10.000 10 1 10.000 10 1 10.300 10.3 1 10.900 10.9 1 10.897 10.897 1 10.017 10.017 1 0.000 0 (7 rows affected) -- ```

SOLUTION 2

 ```-- SELECT * , CAST(Vals AS FLOAT(53)) NewVals FROM RemoveTrailingZeros -- ```

OUTPUT – 2

 ```-- Id Vals NewVals ------ --------------------------------------- ---------------------- 1 10.000 10 1 10.000 10 1 10.300 10.3 1 10.900 10.9 1 10.897 10.897 1 10.017 10.017 1 0.000 0 (7 rows affected) -- ```

SOLUTION 3

 ```-- SELECT * , CAST(Vals AS DOUBLE PRECISION) NewVals FROM RemoveTrailingZeros -- ```

OUTPUT – 3

 ```-- Id Vals NewVals ------ --------------------------------------- ---------------------- 1 10.000 10 1 10.000 10 1 10.300 10.3 1 10.900 10.9 1 10.897 10.897 1 10.017 10.017 1 0.000 0 (7 rows affected) -- ```

SOLUTION 4

 ```-- SELECT * , CONVERT(DOUBLE PRECISION, Vals) NewVals FROM RemoveTrailingZeros -- ```

OUTPUT – 4

 ```-- Id Vals NewVals ------ --------------------------------------- ---------------------- 1 10.000 10 1 10.000 10 1 10.300 10.3 1 10.900 10.9 1 10.897 10.897 1 10.017 10.017 1 0.000 0 (7 rows affected) -- ```

You can also use REAL. REAL = FLOAT(24)
The ISO synonym for real is float(24).

SOLUTION 5

 ```-- SELECT * , CONVERT(REAL, Vals) NewVals FROM RemoveTrailingZeros -- ```

OUTPUT – 5

 ```-- Id Vals NewVals ------ --------------------------------------- ---------------------- 1 10.000 10 1 10.000 10 1 10.300 10.3 1 10.900 10.9 1 10.897 10.897 1 10.017 10.017 1 0.000 0 (7 rows affected) -- ```

Enjoy 🙂

Pawan Khowal

Pawan is a SQL Server Developer. If you need any help in writing code/puzzle or training please email at – pawankkmr”AT”gmail.com. Meanwhile please go throgh the top pages from his blog.

Page Detail URL
☛ SQL Tricky Queries https://msbiskills.com/sql-puzzles-finding-outputs/
☛ SQL Server Performance tuning Articles https://msbiskills.com/sql-performance-tuning/
☛ SQL Server Articles https://msbiskills.com/t-sql/
☛ SQL Interview Questions & Answers https://msbiskills.com/sql-server-interview-questions/