I have a list of stores with their post codes. What I need to do is when a user types in a postcode on the webpage it search the db and then finds the closest 10 to that postcode.
example of data i have:
ID = 1
Store =Halfords
Address 1 =Unit 6b
Address 2 = Braidfute Retail Park
Address 3 = Lanark
Address 4 =South Lanarkshire
Postal Code = ML11 9AE
Lat = 55.6711692810059
Long = -3.77314400672913
I am able to convert the postcode into longitude and latitude, what query do i need to run to find stores within 10miles in ym table of this cord
Firstly, you’ll need the lat/lon of each postcode and store those in a table along with a geospatial representation (GEOGRAPHY, or GEOMETRY datatype). When a post code is entered by the user, you can the look up that post code in your db to find it’s location.
You’ll also need to store a GEOGRAPHY/GEOMETRY representation of each of the stores lat/lons you currently have.
Based on that, you can use all of SQL Server’s geospatial functionality to perform geospatial searches.
I’ve made available on GitHub a .NET app that will import the free Ordnance Survey postcode dataset (all post codes in GB) into SQL Server, converting all the Eastings/Northings provided by it, into lat/lon and then creating a geography column. I have a full blog post on that here. The end result would be used like this:
Hopefully, this will be a good starting point.