SQL Server中全半角转换函数

来源:互联网 发布:网络唤醒主板设置 编辑:程序博客网 时间:2024/05/17 09:05


CREATE   FUNCTION   F_Convert_CharSet(
 @string  nvarchar(4000)   -- 输入的字符串
 ,@flag  tinyint    -- 转换标识(1:全角转半角;其他:半角转全角)
)   
      RETURNS   nvarchar(2000)
      WITH   ENCRYPTION
AS
BEGIN
    DECLARE   @i                   int
                  ,@ret               nvarchar(2000)
                 
    select   @i=1
    SET   @ret= ' '
    if(@flag=1)
    begin
  while     SUBSTRING(@string,@i,1) <> ' '        
  begin  
   if   UNICODE(SUBSTRING(@string,@i,1))> 255      
   SET     @ret=@ret+NCHAR(UNICODE(SUBSTRING(@string,@i,1))-
           CASE    
            WHEN     SUBSTRING(@string,@i,1)= '。 '     THEN     12244     ELSE         65248                                      
           END     )  
  else
   SET     @ret=@ret+(SUBSTRING(@string,@i,1))          
   SET     @i=@i+1    
  end   
    end
    else
    begin
  while   @i <=len(@string)
  begin
   if   unicode(SUBSTRING(@string,   @i,   1)) <256
    select   @ret   =   @ret   +   NCHAR(unicode(SUBSTRING(@string,   @i,   1))+   0xFEE0   )  
   else
    select   @ret   =   @ret   +   SUBSTRING(@string,   @i,   1)          
   SET   @i=@i+1
  end
    end
   

    return   @ret
end

原创粉丝点击