• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

SQL 2005 – Finding nearest value from a column

Finally got the Lat / Lon bearing working this morning in SQL (well MS SQL 2005) :smile

For those who are intrested here it is - result in whole degrees....

DECLARE @lat1 float
DECLARE @lon1 float
DECLARE @lat2 float
DECLARE @lon2 float

SET @lat1 = 37.286771
SET @lon1 = -97.892715
SET @lat2 = 37.22495
SET @lon2 = -97.95307

DECLARE @y float
DECLARE @x float
DECLARE @dLon float
DECLARE @brng DECIMAL(20,10)
DECLARE @result VARCHAR

SET @lat1 = RADIANS(@lat1)
SET @lat2 = RADIANS(@lat2)

SET @dLon = RADIANS(@lon2 - @lon1)
SET @y = SIN(@dLon) * COS(@lat2)
SET @x = (COS(@lat1) * SIN(@lat2)) - (SIN(@lat1) * COS(@lat2) * COS(@dLon))
SET @brng = (ATN2(@y, @x) * 180) / 3.14159265358979323846
SET @Brng = (@Brng+360) % 360;

SELECT CAST(@brng as INT)
 

Advertisers

Back
Top