Tags

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


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.

You can read more from – https://docs.microsoft.com/en-us/sql/t-sql/data-types/float-and-real-transact-sql?view=sql-server-2017

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 🙂

Please add comment(s) if you have one or multiple solutions in mind. Thank You.

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 Advance Puzzles https://msbiskills.com/tsql-puzzles-asked-in-interview-over-the-years/
☛ 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/

My SQL Groups on Facebook:

1. If you like this post, you may want to join my SQL SERVER Interview Puzzles/Interview Questions on Facebook: https://www.facebook.com/groups/1430882120556342/

2. If you like this post, you may want to join my SQL Server Puzzles on Facebook:
https://www.facebook.com/groups/206594023090781/

My SQL Page on Facebook:

2. For all the updates you may follow my page -> https://www.facebook.com/MSBISkillscom-1602779883299222/

Enjoy !!! Keep Learning

Http://MsbiSkills.com

Advertisements