I am trying to use the haversine function to find the distance between
two points on a sphere, specifically two zip codes in my database. I'm
neither horribly familiar with SQL syntax nor math equations :), so I
was hoping I could get some help. Below is what I'm using and it is,
as best as I can figure, the correct formula. It is not however,
giving me correct results. Some are close, others don't seem right at
all. Any ideas?
SET @lat1 = RADIANS(@lat1)
SET @log1 = RADIANS(@log1)
SET @lat2 = RADIANS(@lat2)
SET @log2 = RADIANS(@log2)
SET @Dlat = ABS(@lat2 - @lat1)
SET @Dlog = ABS(@log2 - @log1)
SET @R = 3956 /*Approximate radius of earth in miles*/
SET @A = SQUARE(SIN(@Dla t/2)) + COS(@lat1) * COS(@lat2) *
SQUARE(SIN(@Dlo g/2))
SET @C = 2 * ATN2(SQRT(@A), SQRT(1 - @A))
/*SET @C = 2 * ASIN(min(SQRT(@ A))) Alternative calculation*/
SET @distance = @R * @C
thnx,
cjrsumner