关于字符串分割函数


ALTER function [dbo].[GetStr]( @str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符号
@index int --取第几个元素
)

returns varchar(1024)

as

begin

declare @location int

declare @start int

declare @next int

declare @seed int

set @str=ltrim(rtrim(@str))

set @start=1

set @next=1

set @seed=len(@split)


set @location=charindex(@split,@str)

while @location<>0 and @index>@next

begin

set @start=@location+@seed

set @location=charindex(@split,@str,@start)

set @next=@next+1

end

if @index>@next
begin
return null

end
if @location =0
begin
select @location =len(@str)+1
end
return substring(@str,@start,@location-@start)

 

posted @ 2020-02-10 11:04  码农飞  阅读(166)  评论(0编辑  收藏  举报