I have weird problem with this code: if I run it like shown below I get error:
An invalid floating point operation occurred.
But if I change parameter @Longitude
to -98.508730 (notice only last digit changed) code works just fine.
The code is supposed to lists properties in @MilesRadius
around some LatLng point.
@Latitude and @Longitude parameters are of the same type as longitude and latitude fields in table Address.
What can I do here? Thanks.
DECLARE @Latitude decimal (10,6);
DECLARE @Longitude decimal (10,6);
DECLARE @MilesRadius int;
SET @Latitude = 29.607654
SET @Longitude = -98.508731
SET @MilesRadius = 5
SELECT ADR.LineOne as address,
ADR.City as city,
ADR.Latitude as latitude,
ADR.Longitude as longitude,
((3959 * acos(cos(radians(@Latitude)) * cos(radians(ADR.Latitude)) * cos(radians(ADR.Longitude) - radians(@Longitude)) + sin(radians(@Latitude)) * sin(radians(ADR.Latitude))))) as distance
FROM Shared.Address ADR
WHERE ADR.Latitude IS NOT NULL AND
ADR.Longitude IS NOT NULL AND
(3959 * acos(cos(radians(@Latitude)) * cos(radians(ADR.Latitude)) * cos(radians(ADR.Longitude) - radians(@Longitude)) + sin(radians(@Latitude)) * sin(radians(ADR.Latitude)))) < @MilesRadius
ORDER BY distance