替换特殊字符

时间:2019-12-20 09:56:30

标签: mysql sql

data = "Qwsdyz_qwrbc_bcD_qwEr"

我需要的是

  1. 删除所有_
  2. 所有字符都在下方
  3. 像这样的(QwsdyzQwrbcBcdQwer)中,所有四个字母的首字母都应为大写字母
  4. 无论在上面的语句中进行的任何更改都不会更改结果,就像我们Qwsdqwqyz_qwrwqeqwebc_bcqwD_qqwwEr_dadakjas这样更改一样,我需要QesdqwqyzQwrwqeqwebcBcqwdQqwwerDasakjas这样的结果

请帮助我进行MySQL编码。

set @data="Qwsdyz_qwrbc_bcD_qwEr";
select lower(SUBSTRING_INDEX(@data,"_",1)) into @data1;
select ucase(left(SUBSTRING_INDEX(@data,"_",2),1)) into @data2;
select lower(SUBSTRING_INDEX(@data,"_",2)) into @data3;
select substring(reverse(SUBSTRING_INDEX(reverse(@data3),"_",1)),2) into @data4; 
select reverse((lower(SUBSTRING_INDEX(@data,"_",3)))) into @data5;
select (reverse(SUBSTRING_INDEX(@data5,"_",1))) into @data6;
select ucase(left(@data6,1)) into @data7;
select substring(@data6,2) into @data8;
select reverse(@data) into @data9;
select reverse(lower(SUBSTRING_INDEX(@data9,"_",1))) into @data10;
select ucase(left(@data10,1)) into @data11;
select substring(@data10,2) into @data12;
select concat(@data1,@data2,@data4,@data7,@data8,@data11,@data12) data;

2 个答案:

答案 0 :(得分:0)

您可以使用此question中提供的拆分功能。使用 UPPER()函数对子字符串进行查找,并找到每个分割的字符串的第一个字符,将其用作大写字符,使用 LOWER()函数将其他字符转换成小写。最后加入 CONCAT()函数。

答案 1 :(得分:0)

这只是一个混乱的concat()substring_index()

select concat(concat(upper(left(lower(substring_index(data, '_', 1)), 1)),
                     lower(substr(lower(substring_index(data, '_', 1)), 2))
                    ),
              concat(upper(left(lower(substring_index(substring_index(data, '_', 2), '_', -1)), 1)),
                     lower(substr(lower(substring_index(substring_index(data, '_', 2), '_', -1)), 2))
                    ),
              concat(upper(left(lower(substring_index(substring_index(data, '_', 3), '_', -1)), 1)),
                     lower(substr(lower(substring_index(substring_index(data, '_', 3), '_', -1)), 2))
                    ),
              concat(upper(left(lower(substring_index(substring_index(data, '_', 4), '_', -1)), 1)),
                     lower(substr(lower(substring_index(substring_index(data, '_', 4), '_', -1)), 2))
                    )
             )
from (select 'Qwsdyz_qwrbc_bcD_qwEr' as data) x

SQL并未针对字符串操作进行优化。如果可能的话,我建议您使用其他工具(例如Python)进行此操作。

Here是db <>小提琴。