0

I am trying to create a SQL function which will take 2 parameters and convert to requested case format.My query is

DELIMITER $$

CREATE FUNCTION Convertcase(
    sentence VARCHAR(50),req_case VARCHAR(50)
) 
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE word VARCHAR(50);
IF req_case = 'upper' THEN
    SET word= UPPER(sentence);
ELSEIF req_case = 'lower' THEN
    SET word= LOWER(sentence);
RETURN (word);
END$$
DELIMITER;

convertcase('test sentence','upper')

but I got error

Query : CREATE FUNCTION Convertcase(      sentence varchar(50),req_case varchar(50)  )   RETURNS VARCHAR(50)  DETERMINISTIC  BEGIN  DECL...
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 '' at line 13
Minu
  • 37
  • 1
  • 5
  • 1
    A function without a context like `select convertcase('test sentence','upper')` won't work. Is this the case? [alternate suggestion](https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=8b9b7adb6769b7d0162f6a06705ff25b) – danblack May 03 '20 at 06:14
  • @danblack I already added the context of selecting function – Minu May 03 '20 at 06:36

1 Answers1

0

You need and end if;

drop function if exists convert_case;
DELIMITER $$
CREATE FUNCTION convert_case(sentence VARCHAR(50),req_case VARCHAR(50)) 
RETURNS VARCHAR(50)
BEGIN
DECLARE word VARCHAR(50);
IF req_case = 'upper' THEN
    SET word= UPPER(sentence);
ELSEif req_case = 'lower' THEN
    SET word= LOWER(sentence);
end if; ########
return word;
END$$
DELIMITER ;
P.Salmon
  • 17,104
  • 2
  • 12
  • 19