MySQL选择大写字母遵循小写的地方

时间:2013-05-30 10:21:18

标签: mysql sql

我有一个表格,其中包含许多格式为:

的名称
Max.Example

我想替换。有空间但我意外地将其替换为空,所以它们都像:MaxMuster

我无法恢复备份或回滚。 我找到的唯一方法就是在大写字母正常之后的任何地方插入一个空格。但那命令是什么?

6 个答案:

答案 0 :(得分:2)

尝试此程序......

create procedure updateName()
begin
  declare cnt, len, val, flag int;
  declare newName, oldName varchar(30);
  select count(*) into cnt from tbl;
  set cnt =cnt-1;
  while cnt >= 0 do
     set flag=0;
     select details into oldName from tbl limit cnt, 1;
     select length(oldname) into len;
     while flag=0 and len > 0 do
        select ascii(substring(oldname, len)) into val;
        if val < 90 then 
          select concat(substring(oldname, 1, len-1), ' ', substring(oldname,len)) into newname;
          update tbl set details = newName where details  = oldname;
          set flag=1;
        end if;
        set len = len - 1;
     end while;
     set cnt = cnt-1;
  end while;

end//

FIDDLE

修改

对于多个帽子char 解决多个问题的问题char

create procedure updateName()
begin
  declare cnt, len, val, flag int;
  declare newName, oldName varchar(30);
  select count(*) into cnt from tbl;
  set cnt =cnt-1;
  while cnt >= 0 do
     set flag=0;
     select details into oldName from tbl limit cnt, 1;
     select length(oldname) into len;
     while len > 1 do
        select ascii(substring(oldname, len)) into val;
        if val < 90 then 
          select concat(substring(oldname, 1, len-1), ' ', substring(oldname,len)) into newname;
          update tbl set details = newName where details  = oldname;
          set oldname=newname;
        end if;
        set len = len - 1;
     end while;
     set cnt = cnt-1;
  end while;

end//

FIDDLE

在运行此程序之前备份你的表..

希望这会有所帮助......

答案 1 :(得分:2)

这是一种蛮力方法:

select ltrim(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(myColumn
                     ,'A',' A')
                     ,'B',' B')
                     ,'C',' C')
                     ,'D',' D')
                     ,'E',' E')
                     ,'F',' F')
                     ,'G',' G')
                     ,'H',' H')
                     ,'I',' I')
                     ,'J',' J')
                     ,'K',' K')
                     ,'L',' L')
                     ,'M',' M')
                     ,'N',' N')
                     ,'O',' O')
                     ,'P',' P')
                     ,'Q',' Q')
                     ,'R',' R')
                     ,'S',' S')
                     ,'T',' T')
                     ,'U',' U')
                     ,'V',' V')
                     ,'W',' W')
                     ,'X',' X')
                     ,'Y',' Y')
                     ,'Z',' Z')
            )
from myTable

答案 2 :(得分:1)

这是一种不同的方法。将UNION查询加强到您列的最大长度。它也有改进的余地。

select details, group_concat(t2.c1) as new_value  from (

Select details, 
case 
  when n = 1 then substr(details,n,1)
  when ascii(substr(details,n,1)) between ascii ('A') and ascii ('Z') 
      then concat (' ', substr(details,n,1))
  else substr(details,n,1)
end as c1

,n
FROM tbl, 

  (select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
    )
  as tbl_1 

where substr(details,n,1) is not null

) as t2
group by details

答案 3 :(得分:1)

我找到了一个基于Mark Ba​​nnister的Bruteforce脚本的答案

UPDATE TABLE
SET COLUMN = ltrim(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(
             replace(COLUMN
                      ,'zA','z A')
                     ,'zB','z B')
                     ,'zC','z C')
                     ,'zD','z D')
                     ,'zE','z E')
                     ,'zF','z F')
                     ,'zG','z G')
                     ,'zH','z H')
                     ,'zI','z I')
                     ,'zJ','z J')
                     ,'zK','z K')
                     ,'zL','z L')
                     ,'zM','z M')
                     ,'zN','z N')
                     ,'zO','z O')
                     ,'zP','z P')
                     ,'zQ','z Q')
                     ,'zR','z R')
                     ,'zS','z S')
                     ,'zT','z T')
                     ,'zU','z U')
                     ,'zV','z V')
                     ,'zW','z W')
                     ,'zX','z X')
                     ,'zY','z Y')
                     ,'zZ','z Z')
            );

这适用于所有事情。

MaxExample = Max Example
MaxExampleTest = Max Example Test
MaxExampleTestTT = Max Example Test TT

对所有字母重复这26次。

答案 4 :(得分:0)

一个小小的开始是使用REGEX检查一个字母后跟大写字母(移动到WHERE):

SELECT field REGEXP BINARY '[a-z]{1}[A-Z]{1}' FROM test

但同样,这不是一个完整的答案,更多的只是一个想法。

答案 5 :(得分:0)

这种可怕的蛮力方式可能有助于这种情况。使用巨型案例查找第一个大写字母的位置:

select (case when pos is null then field
             else concat(substr(field, 1, pos - 1), '.', substr(field, pos))
from (select (case when ascii(substr(col, 2, 1)) between ascii('A') and ascii('Z') then 2
                   when ascii(substr(col, 3, 1)) between ascii('A') and ascii('Z') then 3
                   when ascii(substr(col, 4, 1)) between ascii('A') and ascii('Z') then 4
                   when ascii(substr(col, 5, 1)) between ascii('A') and ascii('Z') then 5
                   when ascii(substr(col, 6, 1)) between ascii('A') and ascii('Z') then 6
                   when ascii(substr(col, 7, 1)) between ascii('A') and ascii('Z') then 7
                   when ascii(substr(col, 8, 1)) between ascii('A') and ascii('Z') then 8
                   when ascii(substr(col, 9, 1)) between ascii('A') and ascii('Z') then 9
                   when ascii(substr(col, 10, 1)) between ascii('A') and ascii('Z') then 10
              end) as pos, field
      from t
     ) t

我不确定您是否需要相应的update声明,或者只是找到该期间位置的逻辑。