Created
October 18, 2014 14:26
-
-
Save omichelsen/03efb2febc89f67257a2 to your computer and use it in GitHub Desktop.
Split strings in columns to a new table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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