Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Distance Lookup by Latitude Longitude against all zip codes in table

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-06-04 : 14:25:30
Mason writes "Hi there

I'm trying to figure out how I would write a sql query that will take a given pair of longitude, latitude and compare to a database of longitudes, latitudes. I would want to return all matching where the distance between the two is less than a given variable in miles:

I've taken a rather poor stab at it apparently because it isn't working:

Stored Procedure as follows:

PROC getLatLong
@param1 VARCHAR(30),
@param2 VARCHAR(30)
AS
DECLARE @sql VARCHAR(2000)
DECLARE @C DEC, @varDistance DEC, @varLatitude DEC, @varLongitude DEC
SET @C = sin(47.618371000/57.3) * sin(@param1/57.3) +
cos(47.618371000/57.3) * cos(@param1/57.3) *
cos(@param2/57.3 - -122.203083000/57.3)

SET @varDistance = 3959 * acos(@C)


SET @sql = 'SELECT [' + @param1 + '], [' + @param2 + '] FROM zip_code WHERE 10 > @varDistance'

EXEC(@sql)"
   

- Advertisement -