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, 4:20 AM EDT
Nice article..one doute is there ..i want only the column header..then how to get..?
Sr KarthigaPosted Jul 7, 2016, 3:01 AM EDT
Nice one
Sr KarthigaPosted Jul 7, 2016, 3:01 AM EDT
Good one
Kalpanath ShahapurPosted Aug 16, 2015, 8:10 PM EDT
Thank you it really helpful
Kiran NavasekarPosted Sep 18, 2014, 11:23 PM EDT
Very good ,Thank you
Mansour SHaabanPosted Jun 18, 2014, 10:06 PM EDT
I need to COnvert multible columns to rows not only two columns, Please help
Bhushan LaddePosted Sep 29, 2013, 2:29 AM EDT
How should I proceed for this. I almost stuck in that area.
Bhushan LaddePosted Sep 29, 2013, 2:29 AM EDT
Hi. I am using SQL Server 2008 version and I am getting following error. "Incorrect syntax near the keyword 'for'."
Vaishali APosted Apr 30, 2013, 8:41 PM EDT
thank you Suthish....
Suthish NairTOP 100Posted Apr 30, 2013, 4:57 AM EDT
nope.. this method wont work..
Vaishali APosted Apr 29, 2013, 2:29 AM EDT
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 3, 2013, 10:20 PM EST
hi thanks
Vithal WadjeTOP 100editedPosted Dec 31, 2012, 6:29 PM ESTEdited Dec 31, 2012, 6:30 PM EST
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, 6:18 AM EST
Sir, but how to pivote table if having more than two fields in first table.
Spyros PonarisPosted Dec 21, 2012, 4:17 AM EST
Great , Pivot in sql are very usefull and especially for reporting.
Rakesh BabuPosted Dec 20, 2012, 6:35 PM EST
Thanks a Lot
Mohamed Hussain APosted Dec 20, 2012, 12:00 AM EST
Nice Script !!! Thanks a Lot :)
MadanPosted Nov 20, 2012, 2:56 AM EST
hi suthish, how to find the column count for the above execute(@convert)
Vithal WadjeTOP 100Posted Nov 18, 2012, 11:21 PM EST
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, 12:53 AM EST
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, 3:23 AM EDT
He is the perfect man with perfect solutions.
div krPosted Aug 8, 2012, 12:12 PM EDT
very nice script ,thanks a lot
Suthish NairTOP 100Posted Jul 28, 2012, 3:07 AM EDT
Thank you very much, vithalarchana. I am happy, the post helped you...
Vithal WadjeTOP 100editedPosted Jul 19, 2012, 3:26 AM EDTEdited Oct 12, 2012, 2:28 AM EDT
Sir it is very useful thanks a lot
karthiPosted Jun 14, 2012, 4:14 AM EDT
Hi Suthis..this is Karthik.....it is very useful..thanks...
RamsPosted Apr 27, 2012, 4:32 AM EDT
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, 3:50 AM EDT
Hi Suthishji! Its really nice and useful article...:)