How to convert table data from horizontal into vertical in sql :-
declare @t table (id int, nm varchar(10), typ varchar(10), color varchar(10), qty int, txt varchar(10))
insert @t
select 1, 'name1', 'type1', 'color1', 10, 'note1' union all
select 2, 'name2', 'type2', 'color2', 20, 'note2'
select up.id
,up.col as sourcecolumn
,up.val as [value]
from (
select id, nm,typ,color,convert(varchar(10), qty) qty,txt
from @t --YOUR TABLE
) d
unpivot (val for col in ([nm], [typ], [color],[qty],[txt])) up
declare @t table (id int, nm varchar(10), typ varchar(10), color varchar(10), qty int, txt varchar(10))
insert @t
select 1, 'name1', 'type1', 'color1', 10, 'note1' union all
select 2, 'name2', 'type2', 'color2', 20, 'note2'
select up.id
,up.col as sourcecolumn
,up.val as [value]
from (
select id, nm,typ,color,convert(varchar(10), qty) qty,txt
from @t --YOUR TABLE
) d
unpivot (val for col in ([nm], [typ], [color],[qty],[txt])) up
Comments
Post a Comment