您好,欢迎访问一九零五行业门户网

MySQL 全角转换为半角

序言: 用户注册时候,录入了全角手机号码,所以导致短信系统根据手机字段发送短信失败,现在问题来了,如何把全角手机号码变成半
序言:
用户注册时候,录入了全角手机号码,,所以导致短信系统根据手机字段发送短信失败,现在问题来了,如何把全角手机号码变成半角手机号码?
1,手机号码全角转换成半角
先查询出来全角半角都存在的手机号码的数据
select a.username ,count(1) as num
from(
 select replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(uu.user_name,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9') as username
 from uc_user uu where uu.`user_name` is not null
)a group by a.username  having (count(1)>1)
;
得到如下重复记录:
(mb.134xx76802x ,
mb.136xx88105x ,
mb.152xx80801x ,
mb.157xx49518x ,
mb.186xx88282x ,
mb.189xx94855x ); )
然后删除掉已经存在半角的全角手机号码记录,不然转换后会有重复的手机号码。
delete from `uc_user`
        where mobile like '%1%'
        and replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(user_name,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9')
        in(mb.134xx76802x ,
mb.136xx88105x ,
mb.152xx80801x ,
mb.157xx49518x ,
mb.186xx88282x ,
mb.189xx94855x );
之后再修改全角手机号码为半角手机号码
 update uc_user  uu
            set uu.`mobile`=replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(uu.`mobile`,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9'),
              uu.`user_name`=replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(uu.user_name,'0','0'),'1','1'),'2','2'),'3','3'),'4','4') ,'5','5'),'6','6'),'7','7') ,'8','8'),'9','9')
            where uu.`mobile` is not null;
2,如何把所以的全角转换成半角
上面只是人为用比较笨拙的10个replace将全角转换成了半角,有没有一种通用的思路或者方法来实现呢?于是google了很多资料,写下如下的存储函数。
delimiter $$
use  csdn $$
create function `csdn`.`func_convert`(p_str varchar(200),flag int)
 returns varchar(200)
begin 
    declare pat varchar(8);
    declare step int ;
    declare i int ;
    declare spc int;
    declare str varchar(200);
set str=p_str;
    if  flag=0 then  /**全角换算半角*/
      set pat= n'%[!-~]%' ;
      set step=  -65248  ;
      set str = replace(str,n' ',n'  ');
    else  /**半角换算全角*/
      set  pat= n'%[!-~]%' ;
      set  step= 65248  ;
      set str= replace(str,n'  ',n' ') ; 
    end if;
set i=locate(pat,str) ;
    loop1:while i>0  do
 /**开始将全角转换成半角*/
      set  str= replace(str, substring(str,i,1), char(unicode(substring(str,i,1))+step));
      set  i=instr(str,pat)  ;
    end while loop1;
    return(str) 
end $$
delimiter ;
3,google出来的sqlserver中的全角半角转换函数。
delimiter $$
create
    /*[definer = { user | current_user }]*/
    function `test`.`u_convert`(@str  nvarchar(4000),@flag  bit )
returns  nvarchar 
begin 
    declare    @pat nvarchar(8);
    declare    @step  integer;
    declare    @i  integer;
    declare    @spc  integer;       
    if  @flag=0
    begin
      select  n'%[!-~]%' into @pat;
      select  -65248  into  @step;
      select  replace(@str,n' ',n'  ') into @str;
    end
    else 
    begin
      select  n'%[!-~]%' into @pat;
      select  65248  into  @step;
      select  replace(@str,n'  ',n' ') into @str; 
    end
    select patindex(@pat  collate latin1_general_bin,@str) into @i;
    while  @i>0  do
      select  replace(@str,  substring(@str,@i,1), nchar(unicode(substring(@str,@i,1))+@step)) into @str;
      select  patindex(@pat  collate  latin1_general_bin,@str) into @i;
    end while
    return(@str) 
end $$
delimiter ;
--------------------------------------分割线 --------------------------------------
ubuntu 14.04下安装mysql
《mysql权威指南(原书第2版)》清晰中文扫描版 pdf
ubuntu 14.04 lts 安装 lnmp nginx\php5 (php-fpm)\mysql
ubuntu 14.04下搭建mysql主从服务器
ubuntu 12.04 lts 构建高可用分布式 mysql 集群
ubuntu 12.04下源代码安装mysql5.6以及python-mysqldb
mysql-5.5.38通用二进制安装
--------------------------------------分割线 --------------------------------------
本文永久更新链接地址:
其它类似信息

推荐信息