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:
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