3

是否有任何 SQL 脚本可以将度/分/秒(保存在 A 列中)转换为小数(保存在新的 B 列中)?我搜索了现有的解决方案,但所有示例均指小数->度/分/秒。我很欣赏任何例子,因为我不擅长 SQL 脚本。

4

4 回答 4

4

以下似乎有效:

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)

或者,如果您更喜欢将其全部作为一种表达方式:

SELECT DMS,
       TRUNC(DMS) +
         (TRUNC((DMS - TRUNC(DMS)) * 100) / 60) +
         (((DMS * 100) - TRUNC(DMS * 100)) * 100) / (60 * 60) AS DECIMAL_DEGREES
  FROM TEST_DMS

至少这些与我可信赖的、生锈的 HP-41C 返回的结果一致。:-)

分享和享受。

于 2012-08-30T11:32:16.103 回答
1

我从事一个项目...我使用脚本来解决 oracle db 上的 DMS 转换问题

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
于 2013-07-26T08:46:56.547 回答
0

我用下面的做同样的事情

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    
于 2018-10-19T08:10:31.443 回答
0

基于@user2621989 的回答,这里测试了他的查询的 SQL Server 版本:

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

退货

longitude       latitude
50.05280000000  15.01390000000
于 2019-11-22T08:55:04.467 回答