CAP_FIRST function gives syntax error - mysql

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 ;

Related

mysql/mariadb - syntax error: creating database function

im having trouble finding the correct syntax i always having error #1064.
error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ';
this is the code;
DELIMITER $$
DROP FUNCTION IF EXISTS formula$$
CREATE FUNCTION formula(iq INT, sq INT, ig INT) RETURNS INT
BEGIN
DECLARE result INT;
SET result = (iq-((sq/30)) * ig;
IF result >= 0 THEN
RETURN result;
ELSEIF result < 0 THEN
RETURN 0;
END IF;
END;
$$
DELIMITER ;
i've searching for the answer in the web nothing i can find. what is the wrong with my syntax?
you have a very small mistake in the code.
DELIMITER $$
DROP FUNCTION IF EXISTS formula$$
CREATE FUNCTION formula(iq INT, sq INT, ig INT) RETURNS INT
BEGIN
DECLARE result INT;
SET result = (iq-((sq/30))) * ig; ---> You missed an closing bracket
(")") here
IF result >= 0 THEN
RETURN result; ELSEIF result < 0 THEN
RETURN 0; END IF; END; $$
DELIMITER ;

MySQL script inside set statement

I got a hint from O.jones and went to create a stored function.
That's what I created:
DELIMITER $$
CREATE FUNCTION change_int(colres VARCHAR(500)) RETURNS INT(11)
BEGIN
DECLARE res int(11);
DECLARE leng int(11);
DECLARE newres int(11);
DECLARE mult int(11);
DECLARE temp1 int(11);
DECLARE temp2 int(11);
SET res = CAST(colres AS UNSIGNED);
SET leng = CHAR_LENGTH(CAST( colres AS CHAR));
SET newres = 0;
SET mult = 1;
SET temp1 = 0;
SET temp2 = 0;
WHILE (res > 0) DO
SET temp1 = MOD(res , 10 );
SET res = (res DIV 10);
SET temp2 = MOD(res , 10 );
SET newres = (newres +((temp1 + temp2 ) * mult));
SET mult = mult*10;
END WHILE;
SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END $$
DELIMITER ;
But I get error when I ty to run it on line 6 :
#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 ''res' = CAST(colres AS CHAR)' at line 6
Added a delimiter, new 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 'SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END' at line 27
Greate now it worked.
But I can not evoke her:
Ran a test with:
SHOW FUNCTION STATUS;
And it exists.
When I try to ran it like this:
UPDATE `table` SET `col1` = function_name(`col1`);
Also tried:
UPDATE `table` SET `col1` = db.function_name(`col1`);
No luck.
You have missing column before END WHILE. It must be END WHILE;
Working example

What's wrong w/ MySql Statement (Function)

I'm using MySql57
What's wrong with this script?(I'm Mysql newbie)
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 '= 'A0001'; ELSE BEGIN gubun1 = LEFT(cur_max,0,1); gu' at line 10
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE cur_max varchar(100);
DECLARE gubun1 varchar(1);
DECLARE gubun2 varchar(100);
DECLARE RTN_VAL varchar(100);
SELECT IFNULL(MAX(gubun_code),'A0001') INTO cur_max from gubun_code;
IF cur_max = 'A0001' THEN RTN_VAL = 'A0001';
ELSE
BEGIN
gubun1 = LEFT(cur_max,0,1);
gubun2 =
LPAD(CONVERT(CONVERT(RIGHT(cur_max,4),UNSIGNED)+1,CHAR),4,'0');
RTN_VAL = CONCAT(gubun1,gubun2);
END
RETURN RTN_VAL;
END $$
DELIMITER ;
There are several errors in the function. I think this might work:
DELIMITER $$
CREATE FUNCTION narae.FN_GET_GUBUN_MAX() RETURNS varchar(100)
BEGIN
DECLARE v_cur_max varchar(100);
DECLARE v_RTN_VAL varchar(100);
SELECT COALESCE(MAX(gubun_code), 'A0001')
INTO v_cur_max
FROM gubun_code;
IF cur_max = 'A0001' THEN
SET v_RTN_VAL = 'A0001';
ELSE
SET v_RTN_VAL = CONCAT(LEFT(cur_max, 1), RIGHT(cur_max, 4) + 1);
END IF;
RETURN v_RTN_VAL;
END $$
DELIMITER ;
I would further simplify this by doing all the logic in the SQL statement, but this seems to be in the spirit of your solution.

mysql stored procedure declaration throws error on execution

I"m continiously receiving this error when trying to create this stored procedure. I'm trying to write a procedure that splits a comma delimited string. Similar to explode. I feel I'm close.
This is the 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 'DECLARE start_pos, end_pos INT;
SET start_pos = 1;
SET end_pos = Locat' at line 6
I copied the logic from a SQL Server example and did my best translate it to MySql syntax.
This is the entire procedure from start to finish. I'm hoping a well trained eye can explain why I'm getting an error.
DELIMITER $$
CREATE procedure split_string (in p_string_to_split VARCHAR(255),in p_delimiter CHAR(1) )
BEGIN
DROP TEMPORARY TABLE IF EXISTS split_channel_ids;
CREATE TEMPORARY TABLE split_channel_ids (p_channel_id int);
DECLARE start_pos, end_pos INT;
SET start_pos = 1;
SET end_pos = Locate(p_delimiter, p_string_to_split);
WHILE (start_pos < CHAR_LENGTH(p_string_to_split) + 1) DO
IF (end_pos = 0) THEN
SET end_pos = CHAR_LENGTH(p_string_to_split) + 1;
END IF;
--- INSERT split_channel_ids (p_channel_id)
--- VALUES(SUBSTRING(p_string_to_split, start_pos, end_pos - start_pos)) ;
SET start_pos = end_pos + 1;
SET end_pos = Locate(p_delimiter, p_string_to_split, start_pos);
END WHILE;
-- select * from imob_users;
select * from split_channel_ids;
END $$
DELIMITER ;
DECLARE statements (in MySQL) must be at the beginning of their enclosing BEGIN...END block.
In MS SQL, they can be anywhere; but it is annoying there because they do not have block scope, they have procedure scope, so you can't "re-use" names in independent blocks.

Procedure + Insert

I would like to create a procedure with a while but I have an error.
Here is my procedure:
Delimiter //
create procedure procedure1 ()
BEGIN
Declare i INT;
set i=1;
while i< 74 do
execute immediate 'insert into main values(3,"samples")'
set i=1+1;
end while;
end //
I got this error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that on for the right syntax to use near ''insert into main values(3,"samples")'
Someone has an idea please?
Change your insert query in procedure like below
insert into main values(3,'samples')
Use it like this :
- store your query into a variable
Delimiter //
create procedure procedure1 ()
BEGIN
Declare i INT;
v_Sql VARCHAR2(2000);
set i=1;
v_Sql :='insert into main values(3,"samples")';
while i< 74 do
EXECUTE IMMEDIATE v_Sql ;
set i=1+1;
end while;
end //