590
社区成员
发帖
与我相关
我的任务
分享
if exists(select * from sys.objects where name='split' and type='fn')
drop function split
go
create function split(@val varchar(400),@splitval varchar(30),@flag int)
returns varchar(400)
as begin
declare @tab table(number int identity(1,1),val varchar(50))
if charindex(@splitval,@val)<=0
return @val
declare @location int,@values varchar(400),@reval varchar(100)
select @location=1
while @location<len(@val+@splitval)
begin
select @values=substring(@val,@location,charindex(@splitval,@val+@splitval,@location)-@location)
insert into @tab(val)
values(@values)
set @location=charindex(@splitval,@val+@splitval,@location)+1
end
select @reval=val from @tab where number=@flag
return @reval
end
go
select dbo.split('a,bbb,c',',',2)