SQL Server - Display Column data as Column Header in SQL

Asked By Neha Garg on 03-May-13 03:35 AM

I have a table:

Temp_DesId    Fields              Mandatory     Control

1                  Name              0                  ABC

2                  Description        0                  DEF

3                  Quantity         0                  GHI

4                  Price             0                  JKL


I want to display the details as: Column value in Header

Name            Description                   Quantity                 Price

ABC              DEF                      GHI                      JKL

saranya replied to Neha Garg on 08-May-13 02:19 AM
Hi

Use PIVOT sql query
Sandeep Mittal replied to Neha Garg on 12-May-13 12:23 AM
declare @tab table(Temp_DesId int, Fields varchar(50), Mandatory int, Control varchar(50))
insert into @tab
select 1,'Name',0,'ABC' union all
select 2,'Description',0,'DEF'  union all
select 3,'Quantity',0,'GHI' union all
select 4,'Price',0, 'JKL'
 
select [Name],[Description],[Quantity],[Price]
from ( select   Fields, Control from    @tab) src
pivot (max(Control) for Fields in ([Name],[Description],[Quantity],[Price])) pvt