gpt4 book ai didi

mysql - 在 MySQL 中将 CamelCase 转换为 camel_case

转载 作者:行者123 更新时间:2023-11-28 23:42:46 24 4
gpt4 key购买 nike

有没有办法只使用 mysql 将 CamelCase 转换为 camel_case?我知道我可以在 php 中完成,但我希望能够在 mysql 中完成,因为我需要转换数百万个字符串。

最佳答案

这是一个解决方案:

delimiter $$

drop function if exists replaceCamelCase $$
create function replaceCamelCase( p_str TEXT ) returns text
BEGIN
declare v_pos int;
declare v_len int;
declare cnt int;
declare tmp text;
declare ret text;

set v_pos=1;
set v_len=1+char_length( p_str );
set ret = '';
set cnt = 0;

if p_str REGEXP('[^_]') then
while (v_pos<v_len)
do
set tmp = SUBSTR(p_str, v_pos, 1);

if tmp REGEXP BINARY '[A-Z]' = 1 then

if cnt > 0 then
set ret = concat(ret, '_');
end if;

set ret = concat(ret, lower(tmp));
set cnt = cnt + 1;
else
set ret = concat(ret, tmp);
end if;

set v_pos = v_pos + 1;

end while;

else
set ret = p_str;
end if;

RETURN ret;


end $$

像这样使用它:

SELECT replaceCamelCase(name) FROM `test` WHERE 1

示例输入/输出:

BlaTest
test_test
BaldieBal
TestStringCase

输出:

bla_test
test_test
baldie_bal
test_string_case

关于mysql - 在 MySQL 中将 CamelCase 转换为 camel_case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com