Click here to Skip to main content
15,672,024 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
i have my database with all Zip codes, latitudes and long address, here my requirement is,
if i given Zip code and Radius , i need to get all Zip Codes in that Region from My database only.
i searched it, but i got google API from[^],
i don't need API , i just want my results from my SQL database table only.

Any Ideas,
Thanks In advance
Updated 6-Dec-13 0:20am
Richard MacCutchan 6-Dec-13 6:21am    
You need to write a query command to your database.
dorababu407 6-Dec-13 6:29am    
how can we write command for convert given radius to latitude and longitude .
my input is ZipCode and Radius(km) only.
in my database table has four fields, 1)ZIPCode 2)latitude 3) longitude 4)StateAbbreviation
how can i write code for this ? plz help me
Richard MacCutchan 6-Dec-13 7:00am    
Sorry, but I have no information on how zip codes relate to latitude and longitude. You need to do some research on the subject.

1 solution

reslts in miles

SELECT id, ( 3959 * acos( cos( radians(YOUR_LATITUDE) ) * cos( radians( YOUR_DB_LAT_FIELD ) ) * cos( radians( YOUR_DB_LNG_FIELD )
- radians(YOUR_LONGITUDE) ) + sin( radians(YOUR_LATITUDE) ) * sin( radians( YOUR_DB_LAT_FIELD ) ) ) ) AS distance
FROM YOUR_DB_TABLE HAVING distance < 25 ORDER BY distance ASC;

YOUR_LATITUDE and YOUR_LONGITUDE are where you enter the values of your ZIP code, YOUR_DB_LAT_FIELD and YOUR_DB_LNG_FIELD are the fields containing the longitude and latitude values for each ZIP code in your database, and finally YOUR_DB_TABLE is the database table you are querying. “distance < 25″ tells the database to only return values that are less than 25 miles away. You can modify or omit this to return all.
Share this answer
dorababu407 6-Dec-13 9:37am    
our values for latitude and longitudes are in vachar type, can you please suggest me how to convert them from varchar to geography or how to query the varchar type values with the above query. we are not even able to run the sql query. it is giving invalid columns as my datatype are in varchar type. please let me know a solution to the problem. Thanks
Wombaticus 6-Dec-13 12:00pm    
Well it depends on what database you're using but most will have a CAST or CONVERT function - eg
radians(convert(YOUR_LATITUDE as float)) (or whatever)
Wombaticus 6-Dec-13 12:04pm    
If I was you, I'd convert the lat/long fields in my database to a numeric format, and save the problem of converting them in the SQL query.
dorababu407 6-Dec-13 11:52am    
what is 3959 in the query?
Wombaticus 6-Dec-13 12:00pm    
it's just a constant - it's the way the maths works out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900