将列中的所有文本转换为 MySql 中的驼峰式大小写
Converting all text in a column to Camel Case in MySql
我正在寻找一种方法将我在数据库中的所有文本转换为驼峰式大小写/正确的大小写
即从 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
语句更改查询分隔符。否则,正文中的 ;
结束 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 ;
concat ( upper(substring(name,1,1)), lower(right(name,length(name)-1)))
table_name = cricket_team
column_name = player_names
select player_names, concat(
upper(substring(substring_index(player_names,' ',1),1,1)),
lower(substring(substring_index(player_names,' ',1),2)) , ' ',
upper(substring(substring_index(player_names,' ',-1),1,1)),
lower(substring(substring_index(player_names,' ',-1),2))
) as casing_player_name
from cricket_team;
我正在寻找一种方法将我在数据库中的所有文本转换为驼峰式大小写/正确的大小写
即从 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
语句更改查询分隔符。否则,正文中的 ;
结束 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 ;
concat ( upper(substring(name,1,1)), lower(right(name,length(name)-1)))
table_name = cricket_team
column_name = player_names
select player_names, concat(
upper(substring(substring_index(player_names,' ',1),1,1)),
lower(substring(substring_index(player_names,' ',1),2)) , ' ',
upper(substring(substring_index(player_names,' ',-1),1,1)),
lower(substring(substring_index(player_names,' ',-1),2))
) as casing_player_name
from cricket_team;