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 8702615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:41:50+00:00 2026-06-13T02:41:50+00:00

I have a SQL function that calculates the distance between two points using their

  • 0

I have a SQL function that calculates the distance between two points using their lat/lng coordinates. Using this function, I can shortlist a set of locations from a large set (GEOCODES table) based on their distance for a given point like so:

DECLARE @LAT1 float, @LNG1 float,
SET @LAT1 = '34.169540'
SET @LNG1 = '-92.590990'

SELECT E.* FROM GEOCODES E
WHERE dbo.fnGetDistance(E.lat, E.lng, @LAT1, @LNG1, 'miles') < '20'

I have a second table (GEOMILES) with distances between points in the form:

LOCATION1  | STATE1  |  LOCATION2  |  STATE2  | DISTANCE
-----------+---------+-------------+----------+---------
New York   |   NY    |    Boston   |   MA     |   220

What I’m trying to achieve is a SELECT query that returns a distance of 220 if a departure point is within 20 miles of New York, and an arrival point is within 20 miles of Boston. So far, I have this but it returns nothing when I know from checking manually that the lat/lng points etc add up. Any help is much appreciated!

DECLARE @FROM_LOC VARCHAR(100), @FROM_STA VARCHAR(2), @TO_LOC VARCHAR(100), @TO_STA VARCHAR(2)

SET @FROM_LOC = 'NEWARK'
SET @FROM_STA = 'NJ'

SET @TO_LOC = 'MEDFORD'
SET @TO_STA = 'MA'      

SELECT G.DIST FROM GEOMILES G, GEOCODES E
JOIN GEOCODES C ON C.asciiname = @FROM_LOC AND C.admin1 = @FROM_STA
JOIN GEOCODES D ON D.asciiname = @TO_LOC AND D.admin1 = @TO_STA
WHERE dbo.fnGetDistance(E.lat, E.lng, C.lat, C.lng, 'miles') < '20'
AND dbo.fnGetDistance(E.lat, E.lng, D.lat, D.lng, 'miles') < '20'
  • 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-13T02:41:52+00:00Added an answer on June 13, 2026 at 2:41 am

    I think this is what you’re after (using you table structure). . .

    SELECT G.DIST
    FROM GEOMILES G
    , GEOCODES F --from airport
    , GEOCODES E --to airport
    , GEOCODES C --from location
    , GEOCODES D --to location
    
    WHERE C.asciiname = @FROM_LOC 
    AND C.admin1 = @FROM_STA
    
    AND D.asciiname = @TO_LOC 
    AND D.admin1 = @TO_STA
    
    AND dbo.fnGetDistance(F.lat, F.lng, C.lat, C.lng, 'miles') < 20
    AND dbo.fnGetDistance(E.lat, E.lng, D.lat, D.lng, 'miles') < 20
    
    and G.Location1 = F.asciiname
    and G.Location2 = E.asciiname
    and G.State1 = F.admin1
    and G.State2 = E.admin1
    

    Alternate version (I think this will perform better)

    SELECT G.DIST
    FROM GEOMILES G
    inner join
    (
        select fromAirport.asciiname
        , fromAirport.admin1
        from GeoCodes fromAirport
        where exists
        (
            select top 1 1
            from GeoCodes fromLocation
            where fromLocation.asciiname = @FROM_LOC
            and fromLocation.admin1 = @FROM_STA
            and dbo.fnGetDistance(fromLocation.lat, fromLocation.lng, fromAirport.lat, fromAirport.lng, 'miles') < 20
        )
    ) fromA
        on G.Location1 = fromA.asciiname
        and G.State1 = fromA.admin1
    inner join
    (
        select toAirport.asciiname
        , toAirport.admin1
        from GeoCodes toAirport
        where exists
        (
            select top 1 1
            from GeoCodes toLocation
            where toLocation.asciiname = @TO_LOC
            and toLocation.admin1 = @TO_STA
            and dbo.fnGetDistance(toLocation.lat, toLocation.lng, toAirport.lat, toAirport.lng, 'miles') < 20
        )
    ) toA
        on G.Location1 = toA.asciiname
        and G.State1 = toA.admin1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have the following SQL code which calculates the pearson correlation between two
I have a sql function that includes this code: DECLARE @CodeNameString varchar(100) SELECT CodeName
I have an MS SQL function that is called with the following syntax: SELECT
I have a c# function that needs to write to a SQL image column.
I have a function in SQL server 2008 that takes a string: 'A,B,C,D' and
I have an user defined table function in SQL Server that aggregate data from
I have two T-SQL scalar functions that both perform calculations over large sums of
I have a model that includes attributes like company_name, posted (a date), location (lat/lng
I have a SQL CLR function that receives a SQLString, and I am converting
I'm new to the Entity Framework. I have a SQL Function that returns the

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.