|I am sure I am overlooking something very simple but I can't see it.
I have a database of locations with latitudes and longitudes. I need to be able to query for locations that fall within a given radius from a lat/lon point
The code I am intending to use is: (Showing fixed centre 38,-118 in this version)
SELECT quakeid, (6371 * acos(cos(radians(38)) * cos(radians(latitude)) * cos(radians(longitude) - radians(-118)) + sin(radians(38)) * sin(radians(latitude)))) AS [dkm]
(Derived from Creating a store locator[^])
This works fine and returns just over half a million entries.
My problem is that as soon as I add
HAVING [dkm] <= 50
WHERE [dkm] <= 50
I get the message
Msg 207, Level 16, State 1, Line 3
Invalid column name 'dkm'.
I obviously have not got my SQL glasses on today as I can't seem to resolve this. Can any one make (polite) suggestions?