This article will helps to convert row values to column/fields, either name or headers using PIVOT operator.
Getting Started
Sometimes we need to generate reports which shows row values as column headers or name. Normally developers use a CASE WHEN statement,
do a cursor or for-loop and generate tables on runtime, or use UNION/UNION ALL etc.. Another way will do the logic on code-behind page.
All these different work rounds can be avoid by using "PIVOT Transformation". Consider a sample: I want to list the Top Contributors of C# Corner.
![]()
The output will look like below.

Now, to convert all usernames to columns or fields and show their respective points below. The expected output should looks like below:
![]()
There are many different ways available on the web. But using PIVOT is much simpler and faster.
The below following method used is PIVOT with Dynamic Columns to convert rows to columns.

Query Explained
STUFF function inserts a string to another string.
e.g.:
- SELECT STUFF('Test', 1, 0, '*')
For XML Path is used for concatenating the Strings.From above query, select @columns columns will return output as following..
![]()
Relational operator PIVOT definition from msdn:
"PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output."
Conclusion
Hope this article helps you to convert rows values to columns headers. I will update the article with more complex samples soon.
Download the attachment and test the query. Post comments and rate it. Also, post all the doubts to the forum section.
Thank You !

Praveen G.RPosted Mar 15, 2018, 8:20 AM
Nice article..one doute is there ..i want only the column header..then how to get..?
Sr KarthigaPosted Jul 7, 2016, 7:01 AM
Nice one
Sr KarthigaPosted Jul 7, 2016, 7:01 AM
Good one
Kalpanath ShahapurPosted Aug 17, 2015, 12:10 AM
Thank you it really helpful
Kiran NavasekarPosted Sep 19, 2014, 3:23 AM
Very good ,Thank you
Mansour SHaabanPosted Jun 19, 2014, 2:06 AM
I need to COnvert multible columns to rows not only two columns, Please help
Bhushan LaddePosted Sep 29, 2013, 6:29 AM
How should I proceed for this. I almost stuck in that area.
Bhushan LaddePosted Sep 29, 2013, 6:29 AM
Hi. I am using SQL Server 2008 version and I am getting following error. "Incorrect syntax near the keyword 'for'."
Vaishali APosted May 1, 2013, 12:41 AM
thank you Suthish....
Suthish NairPosted Apr 30, 2013, 8:57 AM
nope.. this method wont work..
Vaishali APosted Apr 29, 2013, 6:29 AM
can pivot be used in oracle 9i?? i tried but its not working....any suggestions how to perform this same thing in oracle 9i?
srikanth ReddyPosted Jan 4, 2013, 3:20 AM
hi thanks
Vithal WadjeeditedPosted Dec 31, 2012, 11:29 PMEdited Dec 31, 2012, 11:30 PM
Atiullah sir use joins and thanks to suthish sir for providing such as great article,by the help of this article i have done my tough tasks within a time
Atiullah SiddiquePosted Dec 31, 2012, 11:18 AM
Sir, but how to pivote table if having more than two fields in first table.
Spyros PonarisPosted Dec 21, 2012, 9:17 AM
Great , Pivot in sql are very usefull and especially for reporting.
Rakesh BabuPosted Dec 20, 2012, 11:35 PM
Thanks a Lot
Mohamed Hussain APosted Dec 20, 2012, 5:00 AM
Nice Script !!! Thanks a Lot :)
MadanPosted Nov 20, 2012, 7:56 AM
hi suthish, how to find the column count for the above execute(@convert)
Vithal WadjePosted Nov 19, 2012, 4:21 AM
suthish sir,how to use where condition in set @convert = 'select * from (select * from Loandt where lnno=123 ) Repayment pivot(sum(Amount) for Lndt in ('+@columns+')) as pivottable i use it but i got syntax error
rachelle cuPosted Nov 17, 2012, 5:53 AM
i would like to make the column the record, and the record to column. For example, Sam Hobbs will be the row name (record 1)while 4804 will be the column name. thanks!
Manish VadukulPosted Sep 12, 2012, 7:23 AM
He is the perfect man with perfect solutions.
div krPosted Aug 8, 2012, 4:12 PM
very nice script ,thanks a lot
Suthish NairPosted Jul 28, 2012, 7:07 AM
Thank you very much, vithalarchana. I am happy, the post helped you...
Vithal WadjeeditedPosted Jul 19, 2012, 7:26 AMEdited Oct 12, 2012, 6:28 AM
Sir it is very useful thanks a lot
karthiPosted Jun 14, 2012, 8:14 AM
Hi Suthis..this is Karthik.....it is very useful..thanks...
RamsPosted Apr 27, 2012, 8:32 AM
hi sutish, iam having one doubt here, i want to know if we could send the execute query data to an existing table. if yes, i would like to have an example of this. Thanks, Ram
Vilas GitePosted May 30, 2011, 7:50 AM
Hi Suthishji! Its really nice and useful article...:)