Ada*_*cha 4 mysql function mysql-error-1064
我正在寻找一种方法将我在数据库中的所有文本转换为Camel Case/Proper Case
即从... CAMEL HAS LEGS
到Camel Has Legs
我在这里找到了一个答案,要求创建一个函数(下面),然后使用该函数转换文本.
我5.6.32-78.1
在共享托管服务器上使用MySQL版本:当我执行以下功能时,我收到错误
#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
Run Code Online (Sandbox Code Playgroud)
我该如何纠正这个错误?我是MySQL的新手
CREATE FUNCTION `proper_case`(str varchar(128)) RETURNS varchar(128)
BEGIN
DECLARE n, pos INT DEFAULT 1;
DECLARE sub, proper VARCHAR(128) DEFAULT '';
if length(trim(str)) > 0 then
WHILE pos > 0 DO
set pos = locate(' ',trim(str),n);
if pos = 0 then
set sub = lower(trim(substr(trim(str),n)));
else
set sub = lower(trim(substr(trim(str),n,pos-n)));
end if;
set proper = concat_ws(' ', proper, concat(upper(left(sub,1)),substr(sub,2)));
set n = pos + 1;
END WHILE;
end if;
RETURN trim(proper);
END
Run Code Online (Sandbox Code Playgroud)
小智 8
concat ( upper(substring(name,1,1)), lower(right(name,length(name)-1)))
Run Code Online (Sandbox Code Playgroud)
您需要使用该DELIMITER
语句来更改查询分隔符.否则,;
身体内部结束CREATE FUNCTION
声明.
请参阅MySQL中的分隔符
DELIMITER $$
CREATE FUNCTION `proper_case`(str varchar(128)) RETURNS varchar(128)
BEGIN
DECLARE n, pos INT DEFAULT 1;
DECLARE sub, proper VARCHAR(128) DEFAULT '';
if length(trim(str)) > 0 then
WHILE pos > 0 DO
set pos = locate(' ',trim(str),n);
if pos = 0 then
set sub = lower(trim(substr(trim(str),n)));
else
set sub = lower(trim(substr(trim(str),n,pos-n)));
end if;
set proper = concat_ws(' ', proper, concat(upper(left(sub,1)),substr(sub,2)));
set n = pos + 1;
END WHILE;
end if;
RETURN trim(proper);
END
$$
DELIMITER ;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7652 次 |
最近记录: |