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

-- 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>1 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

*/

原文地址:https://www.cnblogs.com/accumulater/p/6244479.html