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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:57:03+00:00 2026-06-16T21:57:03+00:00

I have a SQL statement (saved as LocationSearch in Access) that calculates distance between

  • 0

I have a SQL statement (saved as “LocationSearch” in Access) that calculates distance between two points and returns the “Distance” as a generated field.

SELECT Int((3963*(Atn(-(Sin(LATITUDE/57.2958)*
    Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
    Cos([@lng]/57.2958-LONGITUDE/57.2958))/Sqr(-(Sin(LATITUDE/57.2958)*
    Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
    Cos([@lng]/57.2958-LONGITUDE/57.2958))*(Sin(LATITUDE/57.2958)*
    Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
    Cos([@lng]/57.2958-LONGITUDE/57.2958))+1))+2*Atn(1)))*10)/10 AS Distance, *
FROM Locations
ORDER BY (3963*(Atn(-(Sin(LATITUDE/57.2958)*
    Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
    Cos([@lng]/57.2958-LONGITUDE/57.2958))/Sqr(-(Sin(LATITUDE/57.2958)*
    Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
    Cos([@lng]/57.2958-LONGITUDE/57.2958))*(Sin(LATITUDE/57.2958)*
    Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
    Cos([@lng]/57.2958-LONGITUDE/57.2958))+1))+2*Atn(1)));

All the nasty math code you see is what calculates the distance (in miles) in the SQL statement using Latitude and Longitude coordinates.

However, the problem is that the Distance field that is generated by the SQL statement seems to be returned as a string. If I then add SQL code that asks for locations between a distance of 0 and 45 miles, it returns ANY Distance value that starts between “0” and “45”. This includes a location with a distance of “1017” miles. Apparently, the Distance field is a text field, not a number field. So I can’t use the “BETWEEN” statement. I also can’t evaluate using “<” and “>” because it has the same problem.

I saved the SQL query above as a saved query called “LocationSearch”. This way I can run secondary queries against it, like this:

SELECT * FROM LocationSearch WHERE Distance < @MaxDistance

Access will ask for the @lat, @long and @MaxDistance parameters, then the locations will be returned in a recordset, ordered by distance. However, the problem that occurs is when I enter a MaxDistance of 45. With a table containing locations on the West Coast of the US, and a @lat of 47 and a @long of -122 (near Seattle), Access returns the following:

enter image description here

Notice also that the “Distance” field is right-formatted so it appears to be a numeric field, yet for some reason the query returns a location in San Diego, which is 1,017 miles away. My guess is that it was evaluating the Distance field as a text field, and in an ASCII comparison, I believe that “1017” lies between “0” and “45”.

One other thing: I’m using ASP 3.0 (classic) to access this query using JET OLEDB 4.0.

Anyone know how to define the Distance field as a number?

Thanks!

— EDIT —

Using HansUp’s idea from his answer below, I tried this query to force Access to consider the Distance field as a Single precision number:

SELECT * FROM LocationSearch WHERE CSng(Distance) < @MaxDistance

Even this returned the exact same results as before which included the location in San Diego, 1017 miles away.

  • 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-16T21:57:04+00:00Added an answer on June 16, 2026 at 9:57 pm

    If you can’t find a way to return numerical values instead of text from that Duration field expression, use your query as a subquery, then cast Duration in the containing query.

    SELECT CSng(sub.Duration) AS Duration_as_single
    FROM
        (
            -- your existing query --
        ) AS sub
    WHERE CSng(sub.Duration) BETWEEN 0 AND 45
    ORDER BY 1;
    

    That approach also makes for a nicer ORDER BY … if that counts for anything. 🙂

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

Sidebar

Related Questions

I have a sql statement that concatenates two columns, ItemNumber and Name, into one
I have a SQL statement which returns a number of rows and these are
I have a sql statement that uses Difference => http://msdn.microsoft.com/en-us/library/ms188753.aspx I do this in
I have an SQL statement that I'm executing through OleDb, the statement is something
If I have these two Postgres function definitions saved in two seperate .sql files:
I have a form that generates an SQL statement, based on the users choices.
I have sql statement one; select linkscat.id, linkscat.category from linkscat, contentlinks, links where contentlinks.linksid
I have this SQL statement: SELECT * FROM history WHERE (fk_person = 2119) AND
I have a SQL statement like the following: select A from table1, (select B
I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName]

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.