Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Split strings in columns to a new table
declare @table table(id int, strings nvarchar(400))
insert into @table (id, strings)
select 1, 'abc,def,ghi'
union all
select 2, 'jkl'
union all
select 3, 'mno,pqr'
select * from @table
declare @results table(id int, string varchar(400))
declare @id int
declare @strings varchar(400)
set @id = 0
while exists (select * from @table where id > @id)
begin
select top 1 @id = id, @strings = strings
from @table
where id > @id
order by id asc
insert into @results
select @id, data from dbo.Split(@strings, ',')
end
select * from @results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.