Baked from somewhere else, you try it out, you can calculate it, but the correct is not quite understand.
Use [Niaoren]
GO
/****** object:userdefinedfunction [dbo]. [Fngetdistance] Script DATE:2017/7/28 Friday 12:17:56 ******/
SET ANSI_NULLS on
GO
SET QUOTED_IDENTIFIER ON
GO
--Calculates the distance between two coordinate points (longitude, latitude) of the Earth SQL function
--lordbaby
--Finishing: www.aspbc.com
ALTER FUNCTION [dbo]. [Fngetdistance] (@LatBegin Real, @LngBegin Real, @LatEnd Real, @LngEnd Real) RETURNS FLOAT
As
BEGIN
--distance (km)
DECLARE @Distance REAL
DECLARE @EARTH_RADIUS REAL
SET @EARTH_RADIUS = 6378.137-Earth radius
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
Original address: http://www.open-open.com/code/view/1436452727411
Calculate distance in SQL Server based on map latitude and longitude