Calculating distance from latlang --MySQL functional error (#1064) - mysql

I am creating a function for calculating the haversian distance but in i am getting the Mysql syntax error
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 8
DELIMITER $$
CREATE FUNCTION GEODIST (lat1 DOUBLE, lon1 DOUBLE, lat2 DOUBLE, lon2 DOUBLE)
RETURNS DOUBLE
DETERMINISTIC
BEGIN
DECLARE dist DOUBLE;
SET dist = round(acos(cos(radians(lat1)) * cos(radians(lon1)) * cos(radians(lat2)) * cos(radians(lon2)) + cos(radians(lat1)) * sin(radians(lon1)) * cos(radians(lat2)) * sin(radians(lon2)) + sin(radians(lat1)) * sin(radians(lat2))) * 6378.8, 1);
RETURN dist;
END$$

I have tried your code in Mysql,and the same didn't work ;
but instead when I added an extra line it worked,
which was resetting the delimiter again
Kindly check
END$$
DELIMITER ;
And sorry in case if this also goes wrong..But worked fine for me

Related

MySql function tax (calculate IVA)

The problem is i'm writing the function directly from shell(bash)
trying to translate the syntax from some IDE that i don't know !!
The original function is:
CREATE FUNCTION CalcIVA(#Price money)RETURNS money
AS
BEGIN
DECLARE #IVA money
SET #IVA = #Price * 1.16
RETURN #IVA
END;
So changed some syntax to work with mysql> prompt:
CREATE FUNCTION CalculoIVA(x INT)RETURNS INT DETERMINISTIC
BEGIN
DECLARE IVA INT
SET IVA = x *1.16
RETURN IVA
END;
What i'm doing wrong?
ERROR 1064 (42000): 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 'SET IVA = x *1.16 RETURN IVA END' at line 1
Determine what datatypes you want. INT won't work so hot with your 1.16 idea. You lose to rounding to an INT. The below does not use INT
DROP FUNCTION IF EXISTS CalculoIVA;
DELIMITER $$
CREATE FUNCTION CalculoIVA
(x DECIMAL(12,4))
RETURNS DECIMAL(12,4)
DETERMINISTIC
BEGIN
DECLARE IVA DECIMAL(12,4); -- OR FLOAT etc
SET IVA = x *1.16;
RETURN IVA;
END;$$
DELIMITER ;
select CalculoIVA(9.12);
-- 10.5792
If you want the datatypes to be DECIMAL(12,2) or FLOAT just make the necessary changes.
Note: the DELIMITER wrapper is unnecessary for PHPMyAdmin users.

SQL Error: 1064 You have an error in your SQL syntax;

i have a problem with the next code:
create function proc1 (id int)
returns float
begin
declare sum float
select sum = (note1+note2+note3)/3 from test1.note where note.id=id;
return sum;
end
I got this error :
#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 'select sum = (note.note1+note.note2+note.note3)/3 from test1.note where note.id=' at line 5
I've spent much time searching for a solution but no solution was found :(
delimiter |
create function proc1 (id int)
returns float
begin
declare sum float;
select (note1+note2+note3)/3 into sum from test1.note where note.id=id;
return sum;
end
|
delimiter ;
You had multiple errors:
no delimiter defined. Otherwise the DB thinks your function definition stops at the first ; which is wrong
you need to use select ... into. Otherwise you get a "not allowed to return a resultset from a function" error
you forgot a semicolon after your declare
Use set instead of select or better return without declaring a variable:
delimiter //
create function proc1 (id int)
returns float
begin
return (select (note1+note2+note3)/3 from test1.note where note.id=id);
end
//
delimiter ;
or with variable:
delimiter //
create function proc1 (id int)
returns float
begin
declare sum float;
set sum = (select (note1+note2+note3)/3 from test1.note where note.id=id);
return sum;
end
//
delimiter ;

Mysql 5.6 Function error statement

I try to write mysql function. I have mysql 5.6, and I don`t know why every time i try do it:
CREATE FUNCTION do_it (s INT) RETURNS INT
BEGIN
DECLARE k INT;
SET k= s + 2;
RETURN k;
END;
I get:
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 1 */
I know it is basic mistake, but I don`t know how I can fix it...
You need to change the delimiter that MySQL is using or it will stop processing the CREATE request after the first semi-colon.
Do this:
DELIMITER $$
CREATE FUNCTION do_it (s INT) RETURNS INT
BEGIN
DECLARE k INT;
SET k= s + 2;
RETURN k;
END$$
DELIMITER ;

mysql - function not executing

Below function is not executing ..i dnt know why..
DELIMITER $$
USE `vcbvb`$$
CREATE DEFINER=`dffgdfgfdgg`#`%` FUNCTION `split_string`(
stringToSplit VARCHAR(256),
SIGN VARCHAR(12),
POSITION INT
)
RETURNS VARCHAR(256);
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(stringToSplit, SIGN, POSITION),LENGTH(SUBSTRING_INDEX(stringToSplit, SIGN, POSITION -1)) + 1), SIGN, '');
END$$
DELIMITER ;
Please help me i am getting the error as below
Error :
Error Code : 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 ';
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(stringToSplit,' at line 6
DELIMITER $$
USE `vcbvb`$$
CREATE DEFINER=`dffgdfgfdgg`#`%` FUNCTION `split_string`(
stringToSplit VARCHAR(256),
SIGN VARCHAR(12),
POSITION INT
)
RETURNS VARCHAR(256);
BEGIN
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(stringToSplit, SIGN, POSITION),LENGTH(SUBSTRING_INDEX(stringToSplit, SIGN, POSITION -1)) + 1), SIGN, ``);
END$$
DELIMITER ;
I think you have to change the quotes from '' to ``

syntax error for mysql declaration of variable

CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
DECLARE x INT DEFAULT 0;
REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END
I get an syntax error:
#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 3
But for me, everything seems to be correct. i really don't have any clue! can anybody help?
thanks
You need to temporarily change the delimiter so the MySQL client doesn't think you're done with your statement when it sees the semicolon on line 3:
DELIMITER //
CREATE PROCEDURE dorepeat(IN p1 INT)
BEGIN
DECLARE x INT DEFAULT 0;
REPEAT SET x = x + 1; UNTIL x > p1 END REPEAT;
END//
DELIMITER ;
Remove the DECLARE, you should be able to just do this:
SET #x = 0;
Also, variables need to be prefixed with the # symbol