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

  • Home
  • SEARCH
  • 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 4265786
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:40:17+00:00 2026-05-21T06:40:17+00:00

I want to perform a query (to pull locations within a given distance from

  • 0

I want to perform a query (to pull locations within a given distance from a specified lat/lng) against a MS SQL 2008 Server, no geo field types:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

This is the haversine formula described here. I’d like to call the query, probably setup as a stored procedure, using LINQ – pulled into an IEnumerable list.

The table essentially looks like this:

Locations Table

  • Id – long PK
  • Name – nvarchar…
  • Lat
  • Lng …

I’m not familiar with the math functions in MS SQL, the query example is MySQL. Will this query port to MS SQL well? Am I asking for problems attempting to do this?

  • 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-21T06:40:18+00:00Added an answer on May 21, 2026 at 6:40 am

    The query you’ve posted should work reasonably well.

    However, if you are in the position where you can change your data schema then SQL Server has some native Geography types – and if you could use these then there are built-in functions and built-in indexing which would allow you to execute this type of query much more efficiently – see http://www.microsoft.com/sqlserver/2008/en/us/spatial-data.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to perform a search query from my rails app, which uses the
I want to perform an SQL query that is logically equivalent to the following:
I have a forum. I want to perform an SQL query so I can
I want to perform a simple sql query but cannot manage to get it
Let's say I want to perform this query: (SELECT a FROM t1 WHERE a=10
I want to perform an operation on a array returned from an ActiveRecord query.
It seems that every time I want to perform a db query, I have
I want to save to a log file some SQL query rails performs, (namely
I want to perform simple validation against multiple fields. Please note these fields are
I want to perform my query on a string in this format, xxxx/xxxx where

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.