我有一个表中的数据,其长度如下所示。如何使用SQL查询将它们转换为十进制数字,如43.360556和-5.848889。请注意。
<body>
答案 0 :(得分:2)
十进制度=度数+分钟/ 60 +秒/ 3600
数学非常简单,所以我会创建一个UDF,但你可以使用这样的东西
示例强>
Declare @YourTable Table (ID int,Lat varchar(25),Lng varchar(25))
Insert Into @YourTable values
(1,'43-21-33.87-N','05-51-43.13-W')
Select A.*
,B.Lat
,C.Lng
From @YourTable A
Cross Apply (
Select Lat=( xDim.value('/x[1]','float')
+xDim.value('/x[2]','float')/60.0
+xDim.value('/x[3]','float')/3600.0
)* IIF(xDim.value('/x[4]','varchar(25)') in ('N','E'),1,-1)
From (Select Cast('<x>' + replace((Select replace(A.Lat,'-','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A
) B
Cross Apply (
Select Lng=( xDim.value('/x[1]','float')
+xDim.value('/x[2]','float')/60.0
+xDim.value('/x[3]','float')/3600.0
)* IIF(xDim.value('/x[4]','varchar(25)') in ('N','E'),1,-1)
From (Select Cast('<x>' + replace((Select replace(A.Lng,'-','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A
) C
<强>返回强>
ID Lat Lng DecLat DecLng
1 43-21-33.87-N 05-51-43.13-W 43.3594166 -5.8619722
EDIT创建了一个UDF(只是为了好玩)
CREATE FUNCTION [dbo].[udf-Geo-DMS-Decimal] (@S varchar(max))
Returns Float
AS
Begin
Return (
Select ( xDim.value('/x[1]','float')
+xDim.value('/x[2]','float')/60.0
+xDim.value('/x[3]','float')/3600.0
)* IIF(xDim.value('/x[4]','varchar(25)') in ('N','E'),1,-1)
From (Select Cast('<x>' + replace((Select replace(@S,'-','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as A
)
End
-- Syntax : Select [dbo].[udf-Geo-DMS-Decimal]('43-21-33.87-N')
-- Syntax : Select [dbo].[udf-Geo-DMS-Decimal]('05-51-43.13-W')