1. 程式人生 > >Sqlserver 計算兩座標距離函式

Sqlserver 計算兩座標距離函式

mark

if exists (select * from dbo.sysobjects where id = object_id(N'UF_ETL_GetDistance') and xtype in (N'FN', N'IF', N'TF'))
    drop function UF_ETL_GetDistance
GO

CREATE FUNCTION UF_ETL_GetDistance
( 
   @sLng    DECIMAL(12,6),
   @sLat    DECIMAL(12,6),
   @eLng    DECIMAL(12,6),
   @eLat    DECIMAL
(12,6) ) RETURNS DECIMAL(12,4) AS BEGIN DECLARE @result DECIMAL(12,4) SELECT @result = 6378137.0*ACOS(SIN(@sLat/180*PI())*SIN(@eLat/180*PI())+COS(@sLat/180*PI())*COS(@eLat/180*PI())*COS((@sLng-@eLng)/180*PI())) RETURN @result/1000 END go