用SQL将连续相同的字符压缩成一个字符





 object_id()   
      fn_unicate
go

  dbo.fn_unicate(
    @string nvarchar(), 
    @target nvarchar()) 
    returns nvarchar()


declare @t (c nvarchar())
declare @i int, @value nvarchar(), @ret nvarchar()

 @i = , @value = left(@string, ), @ret = 

while @i <= len(@string)

     right(@value, ) <> substring(@string, @i, )
    
         @t  @value
         @value = 
    
     @value = @value + substring(@string, @i, ), @i = @i + 


 @ret = @ret +
    case when isnull(@target, ) =   left(c,)
        when charindex(left(c,), @target) >   left(c, )
         c 
     @t
 @ret

go

declare @s nvarchar()
 @s = 
print  + @s
print dbo.fn_unicate(@s, )
print dbo.fn_unicate(@s, )
print dbo.fn_unicate(@s, ) 
print dbo.fn_unicate(@s, ) 

print dbo.fn_unicate(, ) + 
print dbo.fn_unicate(, ) + 
go

  fn_unicate
go
阅读更多
换一批

没有更多推荐了,返回首页