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.
Query
-
- DECLARE @Tab AS TABLE ( Id int, Latitude float, Longitude float );
- INSERT INTO @Tab ( Id, Latitude, Longitude ) VALUES ( 1, 28.7041, 77.1025 )
- INSERT INTO @Tab ( Id, Latitude, Longitude ) VALUES ( 2,26.9124, 75.7873 )
- DECLARE @Point1 geography;
- DECLARE @Point2 geography;
- set @Point1=(SELECT GEOGRAPHY::Point(t.Latitude,t.Longitude,4326) FROM @Tab t WHERE t.Id=1)
- set @Point2=(SELECT GEOGRAPHY::Point(t.Latitude,t.Longitude,4326) FROM @Tab t WHERE t.Id=2)
-
- SELECT @Point1.STDistance(@Point2)/1000 AS Distance_in_KM,@Point1.STDistance(@Point2) AS Distance_in_meters
Output
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.