分享

多个数据项的字符串取指定位置字符

 zww_blog 2012-03-15

多个数据项的字符串取指定位置字符

 243人阅读 评论(0) 收藏 举报

-- Author:  happyflsytone 

-- Date:2008-11-05 14:59:34

-- 创建函数

create function [dbo].[split_str]

(

    @s varchar(8000),      --包含多个数据项的字符串

    @index int,            --要获取的数据项的位置

    @split varchar(10)     --数据分隔符

)

returns varchar(100)

as

begin

    if @s is null return(null)

    begin

       declare @splitlen int

       select @splitlen=len(@split+'A')-2

    end

    while @index>and charindex(@split,@s+@split)>0

    begin

        select @index=@index-1,@s=stuff(@s,1,charindex(@split,@s+@split)+@splitlen,'')

    end

    return(isnull(left(@s,charindex(@split,@s+@split)-1),''))

end

 

--测试示例

select dbo.split_str('1-2-3-4',3,'-')

 

--运行结果

/*

3

*/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多