Hi Jeff. Here's the requested t-sql implementation.
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE Function [dbo].[Distance] (
@Lat1 as decimal(18, 6),
@Long1 as decimal(18, 6),
@Lat2 as decimal(18, 6),
@Long2 as decimal(18, 6))
Returns decimal(18, 6) With SchemaBinding
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 @EarthRadius as decimal(18, 6)
Set @EarthRadius = 3956.0 /* miles */
Declare @dDistance as decimal(18, 6)
Set @dDistance = @EarthRadius * @C
Return (@dDistance)
End