数据库
首页 > 数据库> > 将列中的所有文本转换为MySql中的Camel Case

将列中的所有文本转换为MySql中的Camel Case

作者:互联网

我正在寻找一种方法将我在数据库中的所有文本转换为Camel Case / Proper Case

即从CAMEL HAS LEGS到Camel Has Legs

我找到了一个answer here,它要求创建一个函数(下面),然后使用该函数转换文本.

我在共享托管服务器上使用MySQL版本:5.6.32-78.1.当我执行以下功能时,我收到错误

#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 

我该如何纠正这个错误?我是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

解决方法:

您需要使用DELIMITER语句来更改查询分隔符.否则,;在body内部结束CREATE FUNCTION语句.

Delimiters in 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 ;

标签:mysql-error-1064,function,mysql
来源: https://codeday.me/bug/20190823/1700860.html