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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:19:39+00:00 2026-05-13T11:19:39+00:00

I am trying to build a query, but I am having some difficulty. I

  • 0

I am trying to build a query, but I am having some difficulty.

I have a SQL Server 2008 database with a table that includes, among other fields, a geography field that describes road segments. (This data has been imported from TIGER/Line data from the U.S. Census.)

I have another fixed point describing a user’s location. I want to find the closest road segment in the database to that point, but I can’t seem to figure out how to accomplish this. Furthermore, I want to find the closest point on that segment to the user location point. This is what I want to select and return back in my query.

Does anyone have any experience with the geography/geometry functionality that can help me?

Thanks!

  • 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-05-13T11:19:40+00:00Added an answer on May 13, 2026 at 11:19 am

    You can store your objects in a GEOGRAPHY column and create a SPATIAL INDEX over this column.

    Unfortunately, SQL Server implements spatial indexes by tiling the surface and storing the tile identifiers in a plain B-Tree index, so plain ORDER BY STDistance won’t work (well, it will work but won’t use the index).

    Instead, you’ll have to make a query similar to this:

    DECLARE @mypoint GEOGRAPHY
    SET @mypoint = geography::STGeomFromText('POINT(@mylat, @mylon)', 4326);
    
    WITH    num (distance) AS
            (
            SELECT  1000
            UNION ALL
            SELECT  distance + 1000
            FROM    num
            WHERE   distance <= 50000
            )
    SELECT  TOP 1 m.*
    FROM    num
    CROSS APPLY
            (
            SELECT  TOP 1 *
            FROM    mytable
            WHERE   myroad.STDistance(@mypoint) <= distance
            ORDER BY
                    STDistance(@mypoint)
            ) m
    

    This way, SQL Server will first search roads within 1 kilometer from your point, then within 2 kilometers, etc., each time using the index.

    Update:

    If you have multiple points in a table and want to find the closest point for each of them:

    WITH    num (distance) AS
            (
            SELECT  1000
            UNION ALL
            SELECT  distance + 1000
            FROM    num
            WHERE   distance <= 50000
            )
    SELECT  mp.mypoint, m.*
    FROM    @mypoints mp
    CROSS APPLY
            (
            SELECT  TOP 1 m.*
            FROM    num
            CROSS APPLY
                    (
                    SELECT  TOP 1 *
                    FROM    mytable
                    WHERE   myroad.STDistance(@mypoint) <= distance
                    ORDER BY
                            STDistance(@mypoint)
                    ) m
            ) m
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using sql server 2008 and I'm trying to build a query for
I'm trying to build a query for a medical database that counts the number
I am trying to build a query such that some column are built up
I am trying to build correct query for my test server, and i am
I am trying to build a query that will select based on a DateTime
Greetings all, I have a question. I am trying to build a parametrized query
I'm still trying to get the hang of SQL. I built 1 query that
I'm trying build an App Engine connected Android application and am having some problems
I'm trying to build a PostGIS query that uses an alias of the JOIN
I'm trying to build a query that will order results by date and time,

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.