Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Wednesday, January 07, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

TSQL Code Snip: Searching for Locations within a Radius of a Zipcode
8/17/2005 12:48:24 PM

In a previous article we provided a code snippet in ASP on how to conduct a search based on two criteria from the user, a postal code, and a miles for searching within a radius. In this article we provide the same functionality, but all within TSQL.

In a previous article we provided a code snippet in ASP on how to conduct a search based on two criteria from the user, a postal code, and a miles for searching within a radius. In this article we provide the same functionality, but all within TSQL.

This solution provides some benefits over the previous version, instead of running the calculation in the ASP code, and sending a request for a long list of matching zipcodes, we do the entire calculation in the stored procedure. This reduces the amount of requests and traffic needed to sent over the network from our ASP application.

ALTER PROCEDURE dbo.GetLocations
(
@ZipCode
char(11),
@Miles
int
)
AS
/* SET NOCOUNT ON */
Declare @HighLatitude float
Declare @LowLatitude float
Declare @HighLongitude float
Declare @LowLongitude float
Declare @StartLatitude float
Declare @StartLongitude float
Declare @LatitudeRange float
Declare @LongitudeRange float

SELECT @StartLatitude = Latitude, @StartLongitude = Longitude FROM POSTALCODES WHERE POSTALCODE = @Zipcode
Set @LongitudeRange = @Miles / (((Cos(@StartLatitude * pi() / 180) * 6076.0) / 5280.0) * 60)
Set @LatitudeRange = @Miles / 69.045454545454545454545454545455
Set @LowLatitude = @StartLatitude - @LatitudeRange
Set @HighLatitude = @StartLatitude + @LatitudeRange
Set @LowLongitude = @StartLongitude - @LongitudeRange
Set @HighLongitude = @StartLongitude + @LongitudeRange

SELECT * FROM CompanyLocations WHERE PostalCode IN(SELECT PostalCode FROM PostalCodes WHERE (Latitude <= @HighLatitude AND Latitude >= @LowLatitude)  AND (Longitude <= @HighLongitude AND Longitude >= @LowLongitude))

RETURN

Granted this is more of grid search and may return some records that are outside of the parameters by a few miles. However, performance is high, rather than doing a record by record calculation for accuracy. In applications where a high amount of accuracy is needed, we do not recommend this code.

Zipcodes for both US and Canada are available on the Web that will provide you with latitude and longitude values that correspond to the postal codes for under $100. You can search on the Web for these vendors.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 10/3/2008 2:40:38 PM by Anonymous
Comments: Would be even greater if it could return the actual Distance - we can handle sorting in our app but not without the actual data ;)
Left on 7/27/2008 1:01:40 AM by Anonymous
Comments:
Left on 6/19/2006 3:52:37 PM by Anonymous
Comments: It would be great if this could sort the locations by distance.
No ratings available.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970