Fork me on Gitee

sqlserver去掉字符串结尾的全角空格并用半角替换

CREATE FUNCTION [dbo].[FDT_DELETE_ZK_SPACE](
@str nvarchar(4000)
) returns nvarchar(4000)
as
 
begin
   declare @endp int,@endflag int,@length int,@i int,@j int, @appstr nvarchar(4000)
   ,@reversestr nvarchar(4000),@return_str nvarchar(4000)
   select @endflag = 0,@length=len(@str),@i=1,@reversestr = reverse(@str),@endp = 0
   if(SUBSTRING(@reversestr,1,1) = ' ')
   begin
     return @str
   end
   while (@endflag = 0 and @i<=@length)
   begin
      if(SUBSTRING(@reversestr,@i,1) != ' ')
        begin
          set @endflag = 1
          set @endp = @length - @i + 1
        end
        set @i = @i + 1
   end
   set @return_str = substring(@str,1,@endp)
   set @j=1  
   set @appstr= ''
   while(@j<=2*(@i-2))
   begin
      set @appstr = @appstr + ' '
      set @j = @j + 1
   end
   set @return_str = @return_str + @appstr
   return @return_str
end

posted @ 2015-11-24 17:10  JoePotter  阅读(1495)  评论(0编辑  收藏  举报
``