Sql手写split函数

if exists(select id from sysobjects where [name]='splitfun')
begin
 drop function splitfun
end
go
create function splitfun
(
@str varchar(1000),
@split varchar(10)
) returns @a table(col varchar(20))--只是一张虚表
as
begin
 --declare @index int
 declare @temp varchar(20)
 while (charindex(@split, @str) > 1)
 begin
  set @temp = left(@str, charindex(@split, @str)-1)
  
  set @str = stuff(@str,1,charindex(@split, @str),'')
  insert @a(col)values(@temp)
 end
 insert @a(col)values(@str)
 return
end
go

select * from dbo.splitfun('AAA@BBB@ccc@ddd@eee','@')

你可能感兴趣的:(split)