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 ;
Related
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 ;
I ask because I don't know where is the error.
I need to create a function in MySQL. I'm using PhpMyAdmin.
CREATE FUNCTION `aggiungiPermesso`(matricolaUtenteLoggato VARCHAR(20),nomeNuovoPermesso VARCHAR(20),descrizioneNuovoPermesso VARCHAR(250)) RETURNS VARCHAR(150)
BEGIN
DECLARE hapermesso INT;
SELECT COUNT(*) INTO hapermesso FROM ottiene WHERE matricolaUtenteLoggato = ottiene.Matricola AND ID =(SELECT id FROM permesso WHERE Nome = 'Aggiungi permesso')
IF(hapermesso < 1) BEGIN
RETURN 'Errore. Non hai i permessi.';
END
INSERT INTO `permesso`(`ID`,`Nome`, `Descizione`) VALUES(0,nomeNuovoPermesso,descrizioneNuovoPermesso) RETURN 'Nuova operazione aggiunta con successo.';
END
DELIMITER;
The error is
#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
What is the error?
The error was because you were using wrong syntax for Return and IF. I have corrected your query and it works fine now.
DELIMITER $$
CREATE FUNCTION `aggiungiPermesso`(matricolaUtenteLoggato VARCHAR(20),nomeNuovoPermesso VARCHAR(20),descrizioneNuovoPermesso VARCHAR(250)) RETURNS VARCHAR(150) CHARSET latin1
BEGIN
DECLARE hapermesso INT;
SELECT COUNT(*) INTO hapermesso FROM ottiene
WHERE matricolaUtenteLoggato = ottiene.Matricola AND ID =(SELECT id FROM permesso WHERE Nome = 'Aggiungi permesso');
BEGIN RETURN IF(hapermesso > 1,'Errore. Non hai i permessi.','None');
END;
INSERT INTO `permesso`(`ID`,`Nome`, `Descizione`)
VALUES(0,nomeNuovoPermesso,descrizioneNuovoPermesso);
RETURN 'Nuova operazione aggiunta con successo.';
END$$
DELIMITER ;
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 get a #1064 syntax error on the beginning of line 5's if clause when trying to run the following code in PhpMyAdmin:
DELIMITER ;
CREATE DEFINER=`root`#`localhost` FUNCTION `ANREDE`(geschlecht enum('m','w'), vorname VARCHAR(255), nachname VARCHAR(255)) RETURNS varchar(1023) CHARSET latin1
DETERMINISTIC
BEGIN
IF geschlecht = 'm' THEN RETURN CONCAT_WS('',CONCAT_WS(' ','Sehr geehrter Herr',vorname,nachname),',');
ELSE RETURN CONCAT_WS('',CONCAT_WS(' ','Sehr geehrte Frau',vorname,nachname),',');
END IF;
END
It seems to me the if clause is correct - what could cause such an error?
You can either use DELIMITER or you can rewrite your function to make it one-statement function like so
CREATE FUNCTION `ANREDE`
(
geschlecht enum('m','w'),
vorname VARCHAR(255),
nachname VARCHAR(255)
)
RETURNS VARCHAR(1023) CHARSET latin1
DETERMINISTIC
RETURN CONCAT
(CONCAT_WS(' ',
IF(geschlecht = 'm',
'Sehr geehrter Herr',
'Sehr geehrte Frau'),
vorname,
nachname),
',');
Here is SQLFiddle demo.
You need to change your delimiter. Currently, ; is your delimiter, and the parser interprets your ;, at the end of line 5, as the end of your function creation statement.
Try this:
DELIMITER // -- change the delimiter
CREATE ...
BEGIN
IF ... ; -- this is not the end of the statement
...
END
// -- end of statement
DELIMITER ; -- useless if you issue this in PhpMyAdmin, but a good practice nevertheless
I found this function for capitalizing the first letter of words in a string, but it is giving me a syntax error on line 8 when I try to run it in SQL in PHPMyadmin. Can anyone help me sort out what the problem is?
Here is the code:
CREATE FUNCTION CAP_FIRST (input VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE len INT;
DECLARE i INT;
SET len = CHAR_LENGTH(input);
SET input = LOWER(input);
SET i = 0;
WHILE (i < len) DO
IF (MID(input,i,1) = ' ' OR i = 0) THEN
IF (i < len) THEN
SET input = CONCAT(
LEFT(input,i),
UPPER(MID(input,i + 1,1)),
RIGHT(input,len - i - 1)
);
END IF;
END IF;
SET i = i + 1;
END WHILE;
RETURN input;
END;
And this is the error:
MySQL said: Documentation
#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
The delimiter signals the DB engine the end of your statement. Normally it is ;. But that would end the stored procedure at the first ;. And its definition would be incomplete.
You can change the delimiter and add it to the end of your procedure. After that change the delimiter back to ;
delimiter |
CREATE FUNCTION CAP_FIRST (input VARCHAR(255))
...
END;
|
delimiter ;