【数据库SQL】将全角转换成半角

以下语句为将全角转换成半角,
   执行前请备份。
------------------------------------------------------------------------------
if object_id(N'u_convert',N'FN') is not null
 drop   function u_convert
GO


create   function   u_convert(  
@str   nvarchar(4000),     
@flag   bit              
)
returns   nvarchar(4000)  
AS  
begin  
    declare   
          @pat nvarchar(8),
          @step   int,
          @i   int,
          @spc   int  
    if  @flag=0 
     begin 
       select   @pat=N'%[!-~]%',@step=-65248,  
       @str=replace(@str,N' ',N'   ')  
     end
    else  
     begin
       select   @pat=N'%[!-~]%',@step=65248,  
       @str=replace(@str,N'   ',N' ')  
     end
    set   @i=patindex(@pat   collate LATIN1_GENERAL_BIN,@str)  
    while   @i>0  
       select   @str=replace(@str,  
    substring(
               @str,@i,1), 
               nchar(unicode(substring(@str,@i,1))+@step)),
               @i=patindex(@pat   collate   LATIN1_GENERAL_BIN,@str)  
     return(@str)  
end  
GO
---------------------------------------------------------------------------
update t_rm_vip_info set card_id=dbo.u_convert(card_id,0)
---------------------------------------------------------------------------

你可能感兴趣的:(【数据库】SqlServer)