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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:57:36+00:00 2026-05-16T16:57:36+00:00

I have created a MySQL function to determine if a set of latitude and

  • 0

I have created a MySQL function to determine if a set of latitude and longitude coordinates are within a certain range of another set of latitude and longitude coordinates. However, the function is giving me a syntax error so I cannot test to see if it is working properly. Any help figuring out what is causing the error would be greatly appreciated. The function along with a description of it is written below:

It works by having the starting lat/long coordinates passed to the function. The database contains the rows targa, targb, and targc, that contain the latitude, longitude, and range (respectively) to compare to. The targ column in the database specifies whether or not this row should be checked against for latitude/longitude range.

CREATE FUNCTION inrange(
 lat1 decimal(11, 7), 
 lon1 decimal(11, 7))
 READS SQL DATA
 RETURNS INT(1)
BEGIN
 DECLARE distance decimal(18, 10);

 SET distance = ACOS(SIN(lat1)*SIN(targ2)+COS(lat1)*COS(targ2)*COS(targ3-lon1))*6371;

 IF distance <= targ4 THEN 
  RETURN 1;
 END IF;

 RETURN 0;
END$$

The error that mysql is giving me is:

1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘READS SQL DATA
RETURNS INT(1)
BEGIN
DECLARE distance decimal(18, 10)’ at line 4

I can’t seem to figure out how to get past this error.

Also, if anyone came to this post looking for such a mysql function, I also had another related post:

MySQL Function to Determine Zip Code Proximity / Range

I’m very grateful to the other poster for his function (which served as the inspiration for this one), but I need something more compact. Hence, the function in question.

EDIT: The following code is functional and working. Just remember to set the delimiter to $$. Once again, thanks all for the help.

Working Code:

CREATE FUNCTION inrange(
    lat1 decimal(11, 7), 
    long1 decimal(11, 7),
    lat2 decimal(11, 7),
    long2 decimal(11, 7),
    rng decimal(18, 10))
    RETURNS INT(1)
BEGIN
    DECLARE distance decimal(18, 10);

    SET lat1 = lat1 * PI() / 180.0,
       long1 = long1 * PI() / 180.0,
       lat2 = lat2 * PI() / 180.0,
       long2 = long2 * PI() / 180.0;

    SET distance = ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(long2-long1))*6371;

    IF distance <= rng THEN 
        RETURN 1;
    END IF;

    RETURN 0;
END$$
  • 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-16T16:57:37+00:00Added an answer on May 16, 2026 at 4:57 pm

    You have your RETURNS and READS SQL DATA out of order. RETURNS comes first:

    CREATE FUNCTION inrange(
        lat1 decimal(11, 7), 
        lon1 decimal(11, 7))
        RETURNS INT(1)
        READS SQL DATA
    BEGIN
    

    EDIT: Also, you refer to columns in your database, as you said in your comment. Not only does your function not know to which table those columns belong, but generally, you should not refer to columns from within functions. Instead, add parameters to your function:

    CREATE FUNCTION inrange(
        lat1 decimal(11, 7), 
        long1 decimal(11, 7),
        lat2 decimal(11, 7),
        long2 decimal(11, 7),
        rng decimal(18, 10))
        RETURNS INT(1)
    BEGIN
        DECLARE distance decimal(18, 10);
    
        SET distance = ACOS(SIN(lat1)*SIN(lat2)+COS(lat1)*COS(lat2)*COS(long2-long1))*6371;
    
        IF distance <= rng THEN 
            RETURN 1;
        END IF;
    
        RETURN 0;
    END$$
    

    Then, in your queries, you can pass the column names to the function.

    (Disclaimer: Make sure that I put the lats and longs in the correct places. I think I did.)

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

Sidebar

Related Questions

I am have created function in mysql. This is code of my function DROP
I have this problem whereby, i have created a user defined function within php.
I have created mysql to delete table rows, which have lower DATETIME value than
I have created a MySQL database using MySQL Workbench. It has about 20 tables.
I have created a MySQL table and would like to save the contents of
I have created a php/mysql based chat page.It is like gmail chat.once i click
I have created a test table in MySQL and would like to insert 10
I have created a foreign key with NaviCat (a MySQL application), but the instant
i have created a forum using php/mysql and users login through facebook using php
I am fairly new to php and mysql but I have created a small

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.