首页 > 其他 > 详细

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

时间:2017-01-03 14:32:58      阅读:137      评论:0      收藏: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>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

*/

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

原文:http://www.cnblogs.com/accumulater/p/6244479.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!