Get Distance Between Two Points

First, we create a table and insert two records in this table. We need to enter the value of the latitude and longitude for a point.
  1. ---Create Table and Insert Data---  
  2. DECLARE @Tab AS TABLE ( Id int, Latitude float, Longitude float );  
  3. INSERT INTO @Tab ( Id, Latitude, Longitude ) VALUES ( 1, 28.7041, 77.1025 )   
  4. INSERT INTO @Tab ( Id, Latitude, Longitude ) VALUES ( 2,26.9124, 75.7873 )  
  5. DECLARE @Point1 geography;  
  6. DECLARE @Point2 geography;  
  7. set @Point1=(SELECT GEOGRAPHY::Point(t.Latitude,t.Longitude,4326) FROM @Tab t WHERE t.Id=1)  
  8. set @Point2=(SELECT GEOGRAPHY::Point(t.Latitude,t.Longitude,4326) FROM @Tab t WHERE t.Id=2)  
  9. --Calcultae Distance---  
  10. SELECT @Point1.STDistance(@Point2)/1000 AS Distance_in_KM,@Point1.STDistance(@Point2) AS Distance_in_meters   
In the query given above, we need to insert two points with the latitude and the longitude values. Afterwards, we need to calculate geographical values for each point using "GEOGRAPHY::Point" method and subsequently we need to calculate the distance of a point from another point.