with basedata ( name, addy, ph, grade) as (select name, addy, ph, grade1from sourcetableunion allselect name, addy, ph, grade2from sourcetableunion allselect name, addy, ph, grade3from sourcetableunion allselect name, addy, ph, grade4from sourcetableunion allselect name, addy, ph, grade5from sourcetable)INSERT INTO dest_table( name, addy, ph, grade)select name, addy, ph, gradefrom BaseDatawhere grade is not null;
SELECT name,addy,ph,subject,gradeFROM #test CROSS APPLY (VALUES('subject 1',grade1),('subject 2',grade2),('subject 3',grade3),('subject 4',grade4),('subject 5',grade5)) AS x(subject,grade)