sql字符分割函数
create function f_split(@col1 varchar(8000),@col varchar(2))
returns @t table(col varchar(50))
as
begin
declare @i int
set @i=len(@col1+'a')-2
while charindex(@col,@col1)>0
begin
insert @t values (left(@col1,charindex(',',@col1)-1))
select @col1=stuff(@col1,1,charindex(',',@col1),'')
end
insert @t values (@col1)
return
end
go
declare @s varchar(8000)
set @s='1234567891,1234567892,1234567893,1234567894'
insert tb(字段)
select * from dbo.f_split(@s,',')
drop function f_split
returns @t table(col varchar(50))
as
begin
declare @i int
set @i=len(@col1+'a')-2
while charindex(@col,@col1)>0
begin
insert @t values (left(@col1,charindex(',',@col1)-1))
select @col1=stuff(@col1,1,charindex(',',@col1),'')
end
insert @t values (@col1)
return
end
go
declare @s varchar(8000)
set @s='1234567891,1234567892,1234567893,1234567894'
insert tb(字段)
select * from dbo.f_split(@s,',')
drop function f_split