mssql字符串移除指定长度字符
3413 点击·0 回帖
![]() | ![]() | |
![]() | mssql字符串移除指定长度字符 趁着空闲时间编写删除此字符串指定位置之后的指定数目字符,丰富字符串函数增强sql字符串函数功能,以方便大家使用。 [sql] --删除此字符串指定位置之后的指定数目字符 --@count <=0 之后所有的字符 create function fun_Remove(@txt nvarchar(2000),@index int,@count int) returns nvarchar(2000) as begin www.atcpu.com declare @len int set @len=len(@txt) --@count <=0 之后所有的字符 if @count<1 begin if @index <1 begin return '' end return substring(@txt,0,@index) end if @index+@count<1 begin --删除字符总长度<1 return @txt end else if @index+@count>=@len begin --删除字符从指定位置的长度超过原子符长度 if @index<1 begin return '' end return substring(@txt,0,@index) end -- 移除区域间字符 if @index<1 begin return substring(@txt,@index+1,@len-@index-@count) end return substring(@txt,0,@index)+substring(@txt,@index+@count,@len-@index-@count+1) end go 测试: [sql] select dbo.fun_Remove('http://www.atcpu.com',5,1) [sql] --结果 :http//www.atcpu.com | |
![]() | ![]() |