Is there any SQL script that converts Degrees/Minutes/Seconds (saved in Column A) to Decimals (to be saved in new Column B)? I googled for existing solutions, but all examples refer to Decimals->Deg/Min/Sec. I appreciate any example, because I'm not strong in SQL scripting.
-
1Wait, how are those three values saved in one column? And presumably this is a math problem first, not an SQL one.– Clockwork-MuseAug 29, 2012 at 20:39
-
@X-Zero: Unfortunately,they are saved in one column as e.g. 41.182973. If degrees, minutes and seconds would be saved in separate columns, then I could simply divide minutes by 60 and seconds by 3600.– GusgusAug 29, 2012 at 20:44
-
1How is 41.182973 degrees/minutes/seconds? That's what I would expect to be in decimal.– starkAug 29, 2012 at 21:07
-
@Stark - Agreed, that appears to be decimals. Especially because there are only 60 seconds in a minute (not 73)...– Clockwork-MuseAug 29, 2012 at 22:11
-
@stark: No,it was originally written as 41 18' 29.73. Then this was combined into a single column in SQL table. So, it seems to be confusing, but it's deg/min/sec.– GusgusAug 30, 2012 at 10:05
5 Answers
The following seems to work:
SELECT DMS, DEGREES + (MINUTES / 60) + (SECONDS / (60 * 60)) AS DECIMAL_DEGREES
FROM (SELECT DMS,
TRUNC(DMS) AS DEGREES,
TRUNC((DMS - TRUNC(DMS)) * 100) AS MINUTES,
((DMS * 100) - TRUNC(DMS * 100)) * 100 AS SECONDS
FROM TEST_DMS)
or, if you prefer it all as one expression:
SELECT DMS,
TRUNC(DMS) +
(TRUNC((DMS - TRUNC(DMS)) * 100) / 60) +
(((DMS * 100) - TRUNC(DMS * 100)) * 100) / (60 * 60) AS DECIMAL_DEGREES
FROM TEST_DMS
At the very least these agree with the results returned by my trusty, rusty HP-41C. :-)
Share and enjoy.
I worked on a project...I used script to solve issue on DMS conversion on oracle db
SELECT
CASE
WHEN LENGTH(longitude)=7 THEN ROUND (cast(substr(longitude, 1, 1) as numeric(10,5)) + cast(substr(longitude, 3, 2) as numeric(10,5))/60 + cast(substr(longitude, 6, 2) as numeric(10,5))/3600,4)
WHEN LENGTH(longitude)=8 THEN ROUND (cast(substr(longitude, 1, 2) as numeric(10,5)) + cast(substr(longitude, 4, 2) as numeric(10,5))/60 + cast(substr(longitude, 7, 2) as numeric(10,5))/3600,4)
ELSE
ROUND (CAST (longitude as Numeric(10,5)),4)
END AS LONGITUDE,
CASE
WHEN LENGTH(latitude)=7 THEN ROUND (cast(substr(latitude, 1, 1) as numeric(10,5)) + cast(substr(latitude, 3, 2) as numeric(10,5))/60 + cast(substr(latitude, 6, 2) as numeric(10,5))/3600,4)
WHEN LENGTH(latitude)=8 THEN ROUND (cast(substr(latitude, 1, 2) as numeric(10,5)) + cast(substr(latitude, 4, 2) as numeric(10,5))/60 + cast(substr(latitude, 7, 2) as numeric(10,5))/3600,4)
ELSE
ROUND (CAST (latitude as Numeric(10,5)),4)
END AS LATITUDE
I use the below to do the same
declare @cord varchar (max)
set @cord= '2504N 05534E'
select
case when CHARINDEX(' ', @cord,1)>0 then
case when right(LTrIM(RTRIM(left( @cord, CHARINDEX(' ', @cord,1)-1))),1) = 'N' then
(left(LTrIM(RTRIM(left( @cord, CHARINDEX(' ', @cord,1)-1))),2))
+
(SUBSTRING(LTrIM(RTRIM(left( @cord, CHARINDEX(' ', @cord,1)-1))),3,2)/60.0)
else
-1*(
(left(LTrIM(RTRIM(left( @cord, CHARINDEX(' ', @cord,1)-1))),2))
+
(SUBSTRING(LTrIM(RTRIM(left( @cord, CHARINDEX(' ', @cord,1)-1))),3,2)/60.0)
)
end
end latitude
,case when CHARINDEX(' ', @cord,1)>0 then
case when right(LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ', @cord,1),100))),1) = 'E' then
(left(LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ', @cord,1),100))),3) )
+
(SUBSTRING( LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ', @cord,1),100))),4
,2)/60.0)
else
-1*(
(left(LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ', @cord,1),100))),3) )
+
(SUBSTRING( LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ', @cord,1),100))),4
,2)/60.0)
)
end
end longitude
,geography::Point(
case when CHARINDEX(' ', @cord,1)>0 then
case when right(LTrIM(RTRIM(left( @cord, CHARINDEX(' ',
@cord,1)-1))),1) = 'N' then
(left(LTrIM(RTRIM(left( @cord, CHARINDEX(' ', @cord,1)-1))),2))
+
(SUBSTRING(LTrIM(RTRIM(left( @cord, CHARINDEX(' ',
@cord,1)-1))),3,2)/60.0)
else
-1*(
(left(LTrIM(RTRIM(left( @cord, CHARINDEX(' ', @cord,1)-1))),2))
+
(SUBSTRING(LTrIM(RTRIM(left( @cord, CHARINDEX(' ',
@cord,1)-1))),3,2)/60.0)
)
end
end
,case when CHARINDEX(' ', @cord,1)>0 then
case when right(LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ',
@cord,1),100))),1) = 'E' then
(left(LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ',
@cord,1),100))),3) )
+
(SUBSTRING( LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ',
@cord,1),100))),4 ,2)/60.0)
else
-1*(
(left(LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ',
@cord,1),100))),3) )
+
(SUBSTRING( LTrIM(RTRIM(SUBSTRING( @cord, CHARINDEX(' ',
@cord,1),100))),4 ,2)/60.0)
)
end
end
, 4326)
as geography_Point
Based on @user2621989 answer here is tested SQL Server version of his query:
DECLARE @longitude varchar(max) = '50°03''10"'
DECLARE @latitude varchar(max) = '15°00''50"'
SELECT
CASE
WHEN LEN(@longitude)=8 THEN ROUND (cast(SUBSTRING(@longitude, 1, 1) as numeric(9,6)) + cast(SUBSTRING(@longitude, 3, 2) as numeric(9,6))/60 + cast(SUBSTRING(@longitude, 6, 2) as numeric(9,6))/3600,4)
WHEN LEN(@longitude)=9 THEN ROUND (cast(SUBSTRING(@longitude, 1, 2) as numeric(9,6)) + cast(SUBSTRING(@longitude, 4, 2) as numeric(9,6))/60 + cast(SUBSTRING(@longitude, 7, 2) as numeric(9,6))/3600,4)
ELSE
ROUND (CAST (@longitude as numeric(9,6)),4)
END AS longitude,
CASE
WHEN LEN(@latitude)=8 THEN ROUND (cast(SUBSTRING(@latitude, 1, 1) as numeric(9,6)) + cast(SUBSTRING(@latitude, 3, 2) as numeric(9,6))/60 + cast(SUBSTRING(@latitude, 6, 2) as numeric(9,6))/3600,4)
WHEN LEN(@latitude)=9 THEN ROUND (cast(SUBSTRING(@latitude, 1, 2) as numeric(9,6)) + cast(SUBSTRING(@latitude, 4, 2) as numeric(9,6))/60 + cast(SUBSTRING(@latitude, 7, 2) as numeric(9,6))/3600,4)
ELSE
ROUND (CAST (@latitude as numeric(9,6)),4)
END AS latitude
Returns
longitude latitude
50.05280000000 15.01390000000
Another example if your lat/lng parts are represented in separate columns: hemisphere, degrees, minutes, seconds.
/*
lat/long sign key
+ + : North and East
+ - : North and West
- - : South and West
- + : South and East
*/
SELECT
ROUND(
CASE WHEN LatitudeHemisphere = 'N' THEN 1 ELSE -1 END *
([LatitudeDegrees] +
(CAST([LatitudeMinutes] AS FLOAT)/60) +
(CAST([LatitudeSeconds] AS FLOAT)/3600)), 7, 1) AS Latitude,
ROUND(
CASE WHEN LongitudeHemisphere = 'E' THEN 1 ELSE -1 END *
([LongitudeDegrees] +
(CAST([LongitudeMinutes] AS FLOAT)/60) +
(CAST([LongitudeSeconds] AS FLOAT)/3600)), 7, 1) AS Longitude
FROM [your_table]