zip code distance:
/*
select top 200 * from zips where
state='CA'
select dbo.fn_geoDistance(34.1839,118.77020,21.398000,157.8981)
-- distance between zips
declare @lat1 decimal(9,5)
declare @long1 decimal(9,5)
declare @lat2 decimal(9,5)
declare @long2 decimal(9,5)
select @lat1=lat,@long1=long from
zips where zip='91344'
select @lat2=lat,@long2=long from
zips where zip='90210'
select dbo.fn_geoDistance(@lat1,@long1,@lat2,@long2)
select top 200 *,dbo.fn_geoDistance(@lat1,@long1,lat,long)
from zips where state='CA' and dbo.fn_geoDistance(@lat1,@long1,lat,long)
< 25
*/
CREATE function fn_geoDistance
(
@lat1 decimal(9,5),
@long1 decimal(9,5),
@lat2 decimal(9,5),
@long2 decimal(9,5)
)
returns decimal(9,4)
as
begin
return
(3963.1676*Acos(Sin(radians(@lat1))*Sin(radians(@lat2))+Cos(radians(@lat1))*Cos(radians(@lat2))*Cos(radians(@long2)-radians(@long1))))
end