Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:59:33+00:00 2026-06-13T06:59:33+00:00

I am trying to calculate the distance between two positions on a map. I

  • 0

I am trying to calculate the distance between two positions on a map.
I have stored in my data: Longitude, Latitude, X POS, Y POS.

I have been previously using the below snippet.

DECLARE @orig_lat DECIMAL
DECLARE @orig_lng DECIMAL
SET @orig_lat=53.381538 set @orig_lng=-1.463526
SELECT *,
    3956 * 2 * ASIN(
          SQRT( POWER(SIN((@orig_lat - abs(dest.Latitude)) * pi()/180 / 2), 2) 
              + COS(@orig_lng * pi()/180 ) * COS(abs(dest.Latitude) * pi()/180)  
              * POWER(SIN((@orig_lng - dest.Longitude) * pi()/180 / 2), 2) )) 
          AS distance
--INTO #includeDistances
FROM #orig dest

I don’t however trust the data coming out of this, it seems to be giving slightly inaccurate results.

Some sample data in case you need it

Latitude        Longitude     Distance 
53.429108       -2.500953     85.2981833133896

Could anybody help me out with my code, I don’t mind if you want to fix what I already have if you have a new way of achieving this that would be great.

Please state what unit of measurement your results are in.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:59:34+00:00Added an answer on June 13, 2026 at 6:59 am

    Since you’re using SQL Server 2008, you have the geography data type available, which is designed for exactly this kind of data:

    DECLARE @source geography = 'POINT(0 51.5)'
    DECLARE @target geography = 'POINT(-3 56)'
    
    SELECT @source.STDistance(@target)
    

    Gives

    ----------------------
    538404.100197555
    
    (1 row(s) affected)
    

    Telling us it is about 538 km from (near) London to (near) Edinburgh.

    Naturally there will be an amount of learning to do first, but once you know it it’s far far easier than implementing your own Haversine calculation; plus you get a LOT of functionality.


    If you want to retain your existing data structure, you can still use STDistance, by constructing suitable geography instances using the Point method:

    DECLARE @orig_lat DECIMAL(12, 9)
    DECLARE @orig_lng DECIMAL(12, 9)
    SET @orig_lat=53.381538 set @orig_lng=-1.463526
    
    DECLARE @orig geography = geography::Point(@orig_lat, @orig_lng, 4326);
    
    SELECT *,
        @orig.STDistance(geography::Point(dest.Latitude, dest.Longitude, 4326)) 
           AS distance
    --INTO #includeDistances
    FROM #orig dest
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to calculate the distance between two points. The two points I stored
I am trying to calculate the distance between two map point using Google Map
I am trying to calculate the distance between the first GPS point stored in
I am trying to calculate distance between two corelocation objects like this: float meters
In the code below I am trying to calculate the distance between two cities.
I have a stored procedure which calculates the distance between two coordinate pairs as
In the program below, I am trying to calculate the distance between two points.
I am trying to calculate distance between two matrices.For eg. it goes like this.
I'm trying to calculate a distance between two sets of coordinate points in an
I am trying to calculate the distance between two points (Pins) on the iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.