T-SQL 單位(公尺):
Create function dbo.CalculateDistance(@Lat1 float,@Lon1 float,@Lat2 float,@Lon2 float) returns float
as
begin
declare @ret float, @r float, @A float, @B float, @RA float, @RB float, @RL1 float, @RL2 float;
select @r = 6371138, @A = abs(@Lat1 - @Lat2), @B = abs(@Lon1 - @Lon2), @RA = @A * pi() / 180.0, @RB = @B * pi() / 180.0, @RL1 = @Lat1 * pi() / 180.0, @RL2 = @Lat2 * pi() / 180.0, @ret = cast(2 * @r * asin(sqrt(sin(@RA/2) * sin(@RA/2) + cos(@RL1) * cos(@RL2) * sin(@RB/2) * sin(@RB/2))) as decimal(8,0));
return @ret
end