[mssql]字符串移除指定长度字符

来源:互联网 发布:带着淘宝去古代 编辑:程序博客网 时间:2024/05/18 09:10

趁着空闲时间编写删除此字符串指定位置之后的指定数目字符,丰富字符串函数增强sql字符串函数功能,以方便大家使用。

--删除此字符串指定位置之后的指定数目字符--@count <=0 之后所有的字符create function fun_Remove(@txt nvarchar(2000),@index int,@count int)returns nvarchar(2000)asbegin    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)endgo

测试:

select dbo.fun_Remove('http://www.naoqiu.com',5,1) 
--结果 :http//www.naoqiu.com


原创粉丝点击