使用SQL将度/分/秒转换为小数

时间:2012-08-29 20:37:06

标签: sql

是否有任何SQL脚本将度/分/秒(保存在A列中)转换为小数(要保存在新B列中)?我用Google搜索了现有解决方案,但所有示例都参考了Decimals-> Deg / Min / Sec。我感谢任何一个例子,因为我在SQL脚本方面不够强大。

4 个答案:

答案 0 :(得分:3)

以下似乎有效:

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返回的结果一致。 : - )

分享并享受。

答案 1 :(得分:0)

我参与了一个项目...我使用脚本来解决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

答案 2 :(得分: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    

答案 3 :(得分:0)

基于@ user2621989的答案是经过测试的他查询的SQL Server版本:

// ...
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;

class UserRepository extends EntityRepository implements PasswordUpgraderInterface
{
    // ...

    public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
    {
        // this code is only an example; the exact code will depend on
        // your own application needs
        $user->setPassword($newEncodedPassword);
        $this->getEntityManager()->flush($user);
    }
}

返回

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