/ Published in: SQL
Expand |
Embed | Plain Text
CREATE FUNCTION [dbo].[DistanceBetween] (@Lat1 AS real, @Long1 AS real, @Lat2 AS real, @Long2 AS real) RETURNS real AS BEGIN DECLARE @dLat1InRad AS float(53); SET @dLat1InRad = @Lat1 * (PI()/180.0); DECLARE @dLong1InRad AS float(53); SET @dLong1InRad = @Long1 * (PI()/180.0); DECLARE @dLat2InRad AS float(53); SET @dLat2InRad = @Lat2 * (PI()/180.0); DECLARE @dLong2InRad AS float(53); SET @dLong2InRad = @Long2 * (PI()/180.0); DECLARE @dLongitude AS float(53); SET @dLongitude = @dLong2InRad - @dLong1InRad; DECLARE @dLatitude AS float(53); SET @dLatitude = @dLat2InRad - @dLat1InRad; /* Intermediate result a. */ DECLARE @a AS float(53); SET @a = SQUARE (SIN (@dLatitude / 2.0)) + COS (@dLat1InRad) * COS (@dLat2InRad) * SQUARE(SIN (@dLongitude / 2.0)); /* Intermediate result c (great circle distance in Radians). */ DECLARE @c AS real; SET @c = 2.0 * ATN2 (SQRT (@a), SQRT (1.0 - @a)); DECLARE @kEarthRadius AS real; /* SET kEarthRadius = 3956.0 miles */ SET @kEarthRadius = 6376.5; /* kms */ DECLARE @dDistance AS real; SET @dDistance = @kEarthRadius * @c; RETURN (@dDistance); END
You need to login to post a comment.
