您当前的位置: 首页 >  sql

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

发布时间:2012-05-28 14:35:22 ,浏览量:0

--删除此字符串指定位置之后的指定数目字符
--@count <=0 之后所有的字符
create function fun_Remove(@txt nvarchar(2000),@index int,@count int)
returns nvarchar(2000)
as
begin
    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
测试
select dbo.fun_Remove('http://www.naoqiu.com',5,1)
--结果 :http//www.naoqiu.com
转自: http://blog.csdn.net/zhengdjin/article/details/7579736
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    108697博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0448s