Question

all.

I want to create a mysql function that calculates the haversine distance between two geo-code.

 CREATE FUNCTION haversineDistance
                 (
                  lat1 VARCHAR(256), 
                  long1 VARCHAR(256), 
                  lat2 VARCHAR(256), 
                  long2 VARCHAR(256)
                 )
 RETURNS FLOAT DETERMINISTIC
 BEGIN

      DECLARE radLat1 FLOAT;
      DECLARE radLong1 FLOAT;
      DECLARE radLat2 FLOAT;
      DECLARE radLong2 FLOAT;
      DECLARE dLat FLOAT;
      DECLARE dLong FLOAT;
      DECLARE cordLength FLOAT;
      DECLARE centralAngle FLOAT;
      DECLARE distance FLOAT;

      SET radLat1 = CAST(lat1 AS FLOAT) * PI() / 180;
      SET radLong1 = CAST(long1 AS FLOAT) * PI() / 180;
      SET radLat2 = CAST(lat2 AS FLOAT) * PI() / 180;
      SET radLong2 = CAST(long2 AS FLOAT) * PI() / 180;
      SET dLat = radLat2 - radLat1;
      SET dLong = radLong2 - radLong2;
      SET cordLength = POW(SIN(dLat/2),2)+COS(lat1)*COS(lat2)*POW(SIN(dLon/2),2);
      SET centralAngle = 2 * ATAN2(SQRT(cordLength), SQRT(1-cordLength));
      SET distance = 6367 * centralAngle;

      RETURN distance;
 END

But I got an error like this;

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 '' at line 4

Anyone has an idea?

Thank you in advance.

Was it helpful?

Solution

Okay.

Solved it.

 DELIMITER $$
 CREATE FUNCTION haversineDistance
                 (
                  lat1 VARCHAR(256), 
                  long1 VARCHAR(256), 
                  lat2 VARCHAR(256), 
                  long2 VARCHAR(256))
 RETURNS FLOAT DETERMINISTIC
 BEGIN

      DECLARE radLat1 FLOAT;
      DECLARE radLong1 FLOAT;
      DECLARE radLat2 FLOAT;
      DECLARE radLong2 FLOAT;
      DECLARE dLat FLOAT;
      DECLARE dLong FLOAT;
      DECLARE cordLength FLOAT;
      DECLARE centralAngle FLOAT;
      DECLARE distance FLOAT;

      SET radLat1 = CAST(lat1 AS DECIMAL(10,10)) * PI() / 180;
      SET radLong1 = CAST(long1 AS DECIMAL(10,10)) * PI() / 180;
      SET radLat2 = CAST(lat2 AS DECIMAL(10,10)) * PI() / 180;
      SET radLong2 = CAST(long2 AS DECIMAL(10,10)) * PI() / 180;
      SET dLat = radLat2 - radLat1;
      SET dLong = radLong2 - radLong2;
      SET cordLength = POW(SIN(dLat/2),2)+COS(lat1)*COS(lat2)*POW(SIN(dLon/2),2);
      SET centralAngle = 2 * ATAN2(SQRT(cordLength), SQRT(1-cordLength));
      SET distance = 6367 * centralAngle;

      RETURN distance;
 END

OTHER TIPS

Right now, you're getting an error, due to the semi-colon characters present as line delimiter in your function.

To explain it further, directly quoting from MySQL documentation

By default, mysql recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

This means that you must override the default delimiter temporarily for your program to compile. Enacapsulate your program between

DELIMITER $$
   your code for function
$$

and then give it a try.

EDIT: You can change back the delimiter by specifying the default delimiter again

mysql> DELIMITER ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top