I have the problems with my function which needs to calculate distance between given coordinates. As I can see problem is with negative values, and I am running out of ideas how to solve this, so if someone can help me I will really appreciate it!
DELIMITER $$
CREATE DEFINER=`sfff_user`@`%` FUNCTION `GetUserDistance`(lat VARCHAR (20), lon VARCHAR (20), userLat VARCHAR (20), userLon VARCHAR (20)) RETURNS int(11)
BEGIN
DECLARE distance INT (11);
IF ISNULL(lat) OR ISNULL(lon) OR lat = '' OR lon = '' THEN
RETURN 0;
ELSE
SELECT
3956 * 2 * ASIN(SQRT(POWER(SIN((lat - ABS(userLat)) * PI() / 180 / 2), 2) + COS(lat * PI() / 180) * COS(ABS(userLat) * PI() / 180) * POWER(SIN((lon - userLon) * PI() / 180 / 2), 2)))
INTO
distance;
RETURN distance;
END IF;
END
For example result for this call:
select GetUserDistance(44, 21, 44, 21) as distance; is 0 which is ok
But look at this:
select GetUserDistance('-15.4167', '28.2833', '-15.4167', '28.2833') as distance;
is 2129 which is insane!
So if you can take a look it would be verry nice to have correct function, since I am dying to solve this 🙁
Thanks.
For safe side you should CAST
VARCHARtoDECIMALand as @Scharron said no need ofABStry: