I'm trying to create a function like the following:
CREATE FUNCTION TitleToFileName(title varchar(200)) RETURNS varchar(200)
BEGIN
set title = REPLACE(title,":"," ");
set title=REPLACE(title,"/"," ");
set title=REPLACE(title,"_"," ");
RETURN title;
END
MySQL shows error :
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
I tried using ' instead of " and #title instead of title , but didn't work..
You need to redefine Delimiter to something else (eg: $$), instead of (;).
Also as a safety measure, check if the same name function already exists or not (DROP FUNCTION IF EXISTS)
At the end, redefine the DELIMITER to ;
Try :
DELIMITER $$
DROP FUNCTION IF EXISTS `TitleToFileName`$$
CREATE FUNCTION TitleToFileName(title varchar(200)) RETURNS varchar(200)
BEGIN
set title = REPLACE(title,":"," ");
set title=REPLACE(title,"/"," ");
set title=REPLACE(title,"_"," ");
RETURN title;
END $$
DELIMITER ;
Related
I want to create a function in mysql that replaces html entities or other special characters in a string. But I can't get it to work. I am new in writing functions.
DELIMITER $$
CREATE FUNCTION cleanEntities(s varchar(255))
RETURNS varchar(255)
BEGIN
s = replace(s, '&',char(0x0026 using utf8))
s = replace(s, 'ü', char(0xc3bc using utf8))
RETURN s$$
END
$$
DELIMITER ;
I had to remove the ; in the error message, so the & etc. is not shown as HTML.
Error Message
#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 '= replace(s, '&',char(0x0026 using utf8)) s = replace(s, 'ü', char(0xc' at line 4
DELIMITER $$
CREATE FUNCTION cleanEntities(s varchar(255))
RETURNS varchar(255)
BEGIN
set s = replace(s, '&',char(0x0026 using utf8));
set s = replace(s, 'ü', char(0xc3bc using utf8));
RETURN s;
END $$
DELIMITER ;
I receive the following error when using the below function and can not figure out how to fix it.
Error:
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 ';
RETURN IF(result IS NOT NULL, result, '0');
END' at line 8
Function:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
DECLARE result VARCHAR(255);
SET result = NULL;
IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
ENDIF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
Email would be read in as a varchar or text such as "user#domain.com"
expiration_date is a of type TIMESTAMP
payer_email is of type VARCHAR(255)
I've also tried the following function with a different error being shown.
Error:
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 ';
READS SQL DATA
BEGIN
DECLARE result TIMESTAMP;
SET res' at line 2
Function:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS TIMESTAMP;
READS SQL DATA
BEGIN
DECLARE result TIMESTAMP;
SET result = NULL;
IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
ENDIF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
I want to use either of these functions so that an application can just pass in expiration(email); and get a result if the email is of length 9 or greater or otherwise receive nothing.
UPDATE:
Thank you KAILLINGER. Spent 2 hours working on this, and it was a typo! :(
Correct answer for completeness:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(200)
READS SQL DATA
BEGIN
DECLARE result VARCHAR(200);
SET result = NULL;
IF (LENGTH(email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
END IF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
Your ENDIF; is not good, it should be END IF; :)
I am getting a syntax error with this simple sample script in MySql 5.6.17 :
CREATE FUNCTION sampldb.fn_x(param VARCHAR(20))
RETURNS int
BEGIN
return 1;
END
What am i doing wrong? In Sql workbench, the error is shown in the last 2 lines.
use DELIMITER
DELIMITER //
CREATE FUNCTION sampldb.fn_x(param VARCHAR(20))
RETURNS int
BEGIN
return 1;
END
//
I'm getting the following error whenever i'm trying to create the following function in mysql.
error : 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
And my function is
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
RETURN result;
END
You need to change the delimiter before creating a function. If you don't your CREATE statement terminates at the first semi-colon.
Try this:
DELIMITER $$
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
END IF
RETURN result;
END$$
DELIMITER ;
Thanks to #Ravinder for catching the missing END IF.
I want to create a simple function like below to do an easy task:
DELIMITER $$
CREATE FUNCTION f(key TEXT, str TEXT) RETURNS INT
BEGIN
IF LOCATE(key, str) > 0 THEN
RETURN 1;
ELSE
RETURN 0;
END IF;
END $$
DELIMITER ;
But I got an error:
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 'key T
EXT, str TEXT) RETURNS INT
BEGIN
IF LOCATE(key, str) > 0 THEN
R' at line 1
What's the problem?
The function has a syntax error because key is a reserved word in MySql. Solution: rename the parameter or put key inside backticks like this:
CREATE FUNCTION f(`key` TEXT, str TEXT) RETURNS INT
key is a keyword in mysql, put it between ` or use different name.