Hi,
I want to calculate a distance between two locations using Geograpy in SQL Server.
Thanks in advance.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Thiyagarajan ERank #2,329 · 607 reputationPosted Nov 22, 2016, 9:14 AM EST
For Ex:
CREATE PROCEDURE [dbo].[spGetNearLocations]
@latitude decimal(18,14),
@longtitude decimal(18,14)
AS
BEGIN
SET NOCOUNT ON;
-- @p1 is the point you want to calculate the distance from which is passed as parameters
declare @p1 geography = geography::Point(@latitude,@longtitude, 4326);
SELECT *
,@p1.STDistance([location_geography]) as [DistanceInKilometers]
FROM [tblLocations]
EN