July 28, 2009 at 7:48 am
Alright, I'm trying to do a lookup of the closest place in the database depending on the zipcode you enter. Here I'm manually typing in 15205 but on a bigger scale I'm going to be using a variable there.
I'm getting the lat and lng of the zipcode entered from the zipCodeLookup. Using a simple formula i'm comparing the lat and lng in another table (EatonDirectory) to find the closest location from the spot I'm currently at.
This is what I have so far. From what I see everything should work. Hopefully someone can point me in a better direction because I get the following errors
msg 156, level 15, state 1, Line 2
Incorrect syntax near the keyword 'SELECT'.
msg 102, level 15, state 1, Line 2
Incorrect syntax near ')'.
msg 102, level 15, state 1, Line 5
Incorrect syntax near ')'.
msg 102, level 15, state 1, Line 7
Incorrect syntax near ')'.
SELECT *, (3959 * acos( cos(radians
(SELECT lng FROM zipCodeLookup WHERE zipcode = '15205'))
* cos(radians(lat))
* cos(radians(lng) - radians
(SELECT lat FROM zipCodeLookup WHERE zipcode = '15205'))
+ sin (radians
(SELECT lng FROM zipCodeLookup WHERE zipcode = '15205'))
* sin (radians (lat))))
AS distance FROM EatonDirectory having distance < 100
ORDER BY distance limit 1;[/code]
Any help would be much appreciated.
July 28, 2009 at 8:12 am
I don't think you can use the select statement in the radians function. Try using a variable and see if it works.
DECLARE @lng float
Set @lng = (SELECT lng FROM zipCodeLookup WHERE zipcode = '15205')
SELECT *, (3959 * acos( cos(radians
(@lng))
* cos(radians(lat))
* cos(radians(lng) - radians
(@lng))
+ sin (radians
(@lng))
* sin (radians (lat))))
AS distance
FROM EatonDirectory
HAVING distance < 100
July 28, 2009 at 8:41 am
SELECT *, (3959 * acos( cos(radians
((SELECT lng FROM zipCodeLookup WHERE zipcode = '15205')))
* cos(radians(lat))
* cos(radians(lng) - radians
((SELECT lat FROM zipCodeLookup WHERE zipcode = '15205')))
+ sin (radians
((SELECT lng FROM zipCodeLookup WHERE zipcode = '15205')))
* sin (radians (lat))))
AS distance FROM EatonDirectory having distance < 100
ORDER BY distance limit 1;
I got the radians function to work correctly by adding a second set of () around the select statement. Try that and see if it works for you.
July 28, 2009 at 8:43 am
Thanks a lot, they both worked perfectly.
Unfortunitely I'm now finding my math was bad.
Don't suppose anyone is good at math around here that can help me out
July 28, 2009 at 8:50 am
i have this saved in my snippets,it's supposed to find all zip codes within a given distance, assuming you have a table with zipcode, lattitude,longitude in it:
hope this helps:
/*
Returns zip codes within specified range.
*/
CREATE Procedure sp_ZipCode_Range
(
@ZipCode Numeric(5, 0) = Null,
@Miles Float
)
As
set nocount on
Declare @Latitude Float(10)
Declare @Longitude Float(10)
-- Lookup longitude, latitude for zip codes
Select @Latitude = Latitude, @Longitude = Longitude From State Where Zip = @ZipCode
Select
Zip, Zip_Name,
-- Zip_Name,
Avg(3958.75 * ACos(Sin(@Latitude/57.2958) *
Sin(Latitude/57.2958) +
Cos(@Latitude/57.2958) *
Cos(Latitude/57.2958) *
Cos(Longitude/57.2958 - @Longitude/57.2958))) As Miles
From
State
Lowell
July 28, 2009 at 9:36 am
Thanks a lot, it works perfect.
I needed to create a stored procedure with the code so this was even a +
Thanks everyone for all the help.
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy