I am attempting to get the distance from the latitude and longitude from each address in the company. As I pass in the lat and lon it gets to a certain company and I gets the floating point error. I’ve looked through and none are null or crazy large numbers.
Here is the select that uses the function(below) to pull the distance. It errors inside the function on one of the companies, just not sure which one.
SELECT C.ID,C.Name, a.LAT, a.LON, (select top 1 round(distance,5) from dbo.MSAfromLongLat(a.LAT, a.LON)) as distance
FROM
dbo.vwCompanies AS c INNER JOIN
dbo.Address AS a ON a.ID = c.AddressID LEFT OUTER JOIN
dbo.vwGeneralDemographics AS gen ON c.ID = gen.CompanyID AND gen.ID =
(SELECT MAX(ID) AS Expr1
FROM dbo.vwGeneralDemographics AS gen2
WHERE (gen2.CompanyID = c.ID))
WHERE (c.MemberTypeID = 1) AND (c.ARAMembershipType IN ('m3', 'm5', 'm9', 'm15', 'm30', 'mco', 'mbc', 'mbp', 'mi'))
order by a.lat, a.lon
An invalid floating point operation occurred.
I’ve tried switching them to real. Tried rounding round(,15) inside the acos.
Function: MSAfromLongLat
DECLARE @SourceSinLat float,
@SourceCosLat float,
SourceCosLong float,
CountryCode nvarchar(255)
SET @SourceSinLat = sin(@Lat/57.2958)
SET @SourceCosLat = cos(@Lat/57.2958)
SET @SourceCosLong = @Lon/57.2958
INSERT @retDistance
SELECT top 1
amm.MetroCode, amm.Name,
(3958.75 * acos((@SourceSinLat * sin(Latitude/57.2958) + @SourceCosLat * cos(Latitude/57.2958) * cos(Longitude/57.2958 - @SourceCosLong)))) Distance
FROM
MarketMonitorMsa amm
INNER JOIN [External].dbo.[MSAWithLATLON] msall ON msall.MSA = amm.MetroCode
ORDER BY Distance
2