Thursday 6 April 2017

SQL: Break single records into multiple records based on a value from one of the columns



declare @t table (name varchar(10), items varchar(10))
insert @t 
select 'AAA', '1' union all
select 'BBB', '2,' union all
select 'CCC', '3'

select t.name
       ,'1' + substring(items, 2, 1000) as Items
from   @t t
join   master..spt_values n
       on n.type = 'P'
       and n.number < convert(int, left(items,1))