Funciones SQL Server para calcular distancia entre coordenadas.
if exists (select * from sys.objects where name = 'fxDistanceBetween') drop function fxDistanceBetween; if exists (select * from sys.objects where name = 'fxToRad') drop function fxToRad; go -- Cambio de grados a radianes create function fxToRad(@value float) returns float as begin return @value * pi() / 180; end; go -- Distancia en metros entre coordenadas create function fxDistanceBetween(@lat1 float, @lng1 float, @lat2 float, @lng2 float) returns float begin declare @a float, @b float; declare @r float = 6371 * 1000, -- Radio de la tierra para obtener metros @dLat float = dbo.fxToRad(@lat2 - @lat1), @dLng float = dbo.fxToRad(@lng2 - @lng1); set @a = power(sin(@dLat / 2), 2) + ( power(cos(dbo.fxToRad(@lat2)), 2) * power(sin(@dLng / 2), 2) ); set @b = 2 * atn2(sqrt(@a), sqrt(1 - @a)); return @r * @b; end; go