Finding Distance Between two Locations

The below code will calculate the distance (Displacement) between two locations based on longitude and latitude.

-- =============================================  
-- Author:  V.U.M.Sastry Sagi
-- Create date:  11/30/2010
-- Description: Gets the distance between two locations
-- =============================================  
CREATE FUNCTION [dbo].[getDistanceBetweenLatLon]
    (
      @lat1 DECIMAL(5, 2),
      @long1 DECIMAL(5, 2),
      @lat2 DECIMAL(5, 2),
      @long2 DECIMAL(5, 2)
    )
RETURNS NUMERIC(10, 5)
AS BEGIN  
    DECLARE @x DECIMAL(20, 10)  
    DECLARE @pi DECIMAL(21, 20)  
    DECLARE @distance FLOAT  
    SET @pi = 3.14159265358979323846  
    SET @x = SIN(@lat1 * @pi / 180) * SIN(@lat2 * @pi / 180) + COS(@lat1 * @pi / 180)
        * COS(@lat2 * @pi / 180) * COS(ABS(( @long2 * @pi / 180 ) - ( @long1 * @pi / 180 )))  
    SET @x = ATAN(( SQRT(1 - POWER(@x, 2)) ) / @x)  
    SET @distance = ABS(( 1.852 * 60.0 * ( ( @x / @pi ) * 180 ) ) / 1.609344)  
    RETURN @distance  
   END  
        
   
SiteLock