sql两点之间距离

Posted on 2022-06-29 16:45  LUO-TLZ  阅读(150)  评论(0编辑  收藏  举报

CREATE FUNCTION [dbo].[fnGetDistance]

(@LngBegin REAL, @LatBegin REAL, @LngEnd REAL, @LatEnd REAL)

RETURNS FLOAT

AS

BEGIN

--距离(千米)

DECLARE @Distance REAL

DECLARE @EARTH_RADIUS REAL

SET @EARTH_RADIUS = 6378.137



DECLARE @RadLatBegin REAL,

@RadLatEnd REAL,

@RadLatDiff REAL,

@RadLngDiff REAL



SET @RadLatBegin = @LatBegin *PI()/ 180.0

SET @RadLatEnd = @LatEnd *PI()/ 180.0

SET @RadLatDiff = @RadLatBegin - @RadLatEnd

SET @RadLngDiff = @LngBegin *PI()/ 180.0 - @LngEnd *PI()/ 180.0



SET @Distance = 2 *ASIN(

SQRT(

POWER(SIN(@RadLatDiff / 2), 2)+COS(@RadLatBegin)*COS(@RadLatEnd)

*POWER(SIN(@RadLngDiff / 2), 2)

)

)



SET @Distance = @Distance * @EARTH_RADIUS

RETURN @Distance

END