Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rounded to 4 decimal places, but output shows 4 decimal places + 4 0s in MS SQL Server

I am working on Weather Observation Station 17 in HackerRank.
Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N) in STATION is greater than 38.7780. Round your answer to 4 decimal places.

Table: STATION
Fields: ID, CITY, STATE, LAT_N, LONG_W
where LAT_N is the northern latitude and LONG_W is the western longitude.

My code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT ROUND(LONG_W,4,0) AS low
FROM STATION
WHERE LAT_N = (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);

Output:
70.13780000
The answer is wrong. I looked up this question online and the code looks the same in other answers. I am using MS SQL Server.
The same code works fine on MySQL

>Solution :

The ROUND function will still return the same datatype (+ precision + scale) as the input:

SELECT ROUND(1234.56784444, 4)
-- 1234.56780000

You need CAST(... AS DECIMAL(18, 4)) to make sure that the result contains exact 4 digits:

SELECT CAST(ROUND(1234.56784444, 4) AS DECIMAL(18, 4))
-- 1234.5678
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading