mysql - function not executing - mysql

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 ``

Related

getting syntax error on creating function

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 ;

can't get simple mysql function to work

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 &amp 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, '&amp',char(0x0026 using utf8)) s = replace(s, '&uuml', 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 ;

Create MySQL function statement

I've written a function but it gives me mistake a the second line (create statement) if anyone could help me, I really appreciate:
CREATE FUNCTION GetPrefix (phone_num VARCHAR(30)) RETURNS varchar(30)
deterministic
BEGIN
DECLARE x INT;
DECLARE prefix varchar(30);
SET x = 0;
for prefix in SELECT code
FROM tab_len
while (length(phone_num)) > 0
do
if prefix<>left(phone_num, length(phone_num)-x)
then set x=x+1 ;
else return 1 ;
END while ;
END $$;
and I receive this 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 'for prefix in SELECT code
FROM tab_len while (length(phone_n' at line 9
DELIMITER $$
DROP FUNCTION IF EXISTS GetPrefix $$
CREATE FUNCTION GetPrefix
(
phone_num VARCHAR(30)
)
RETURNS varchar(30)
BEGIN
DECLARE var_x INT DEFAULT 0;
DECLARE var_prefix VARCHAR(100);
SET phone_num = IFNULL(phone_num,'');
-- your logic will go here.
return phone_num;
END$$
DELIMITER ;
SELECT GetPrefix('test');
This is right syntax to write a function in mysql.
check out the differences. Take a look Here

How to create a MySQL create function?

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.

Can any one suggest me how to resolve this problem: Error Code : 1064 in MY SQL 5.5 ver

DELIMITER $$;
DROP FUNCTION IF EXISTS tonumeric $$;
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER; $$
When I executed this function, I am facing this 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 'IF EXISTS tonumeric' at line 1
(0 ms taken)
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 ';
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num' at line 1
(0 ms taken)
Thanks
How about this:
DELIMITER $$
DROP FUNCTION IF EXISTS tonumeric $$
CREATE FUNCTION tonumeric() returns numeric
BEGIN
declare num numeric;
set num = to_number('12');
return num;
END$$
DELIMITER ;
Delimiter is a special command, in that you shouldn't terminate it with a ; -- you're actually setting the delimiter to "$$;", not "$$".