Wednesday, 25 May 2016

SQL function for calculating Distance based on Two L attitude and Longitude

CREATE FUNCTION dbo.GetDistance(@lat1 DECIMAL(18,6), @lat2 DECIMAL(18,6), @lon1 DECIMAL(18,6), @lon2 DECIMAL(18,6))
RETURNS DECIMAL(18,6)
AS
BEGIN
declare @Result DECIMAL(18,6)
 IF  ((@lat1!=@lat2)and(@lon1!=@lon2 ))
    begin
        set @Result= ACOS(SIN(PI()*@lat1/180.0)*SIN(PI()*@lat2/180.0)+COS(PI()*@lat1/180.0)*COS(PI()*@lat2/180.0)*COS(PI()*@lon2/180.0-PI()*@lon1/180.0))*6371    
    end
 ELSE
    begin
           set @Result= 0
    end
return @result
END

No comments:

Post a Comment