sql - How to get the values of comma delimited string into a table? -


i have string @str =11,22 in variable. how can insert table like,

 id      num -------------  1       11  2       22 

here's simple way you're looking although dependent on values being split comma , having no spaces (although can use trim if believe happen):

declare @str varchar(10) = '11,22,33' declare @foobar table (id int identity (1,1), num int)  while (charindex(',', @str) > 0) begin insert @foobar (num) select left(@str, charindex(',', @str) - 1) set @str = (select substring(@str, charindex(',', @str) + 1, len(@str))) end if len(@str) > 0     insert @foobar (num)      select @str  select * @foobar 

and here sql fiddle: http://www.sqlfiddle.com/#!6/0e240/2/0


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -