SQL Pivot with Dynamic Column Names

Found a great solution to writing a complex PIVOT function in SQL on the web!

The solution was lying on a thread at one of the ASP.NET forums where the question was:

how to display row values in columns in sql server 2008

and the solution was provided by M. Atif Nadeem from Pakistan.

He has even taken time to create a SQL Fiddle as well!

Basically his solution allows to write a dynamic column name using STUFF and FOR XML functions and later using this dynamic column names to create a Dynamic SQL Pivot Statement.

How sweet !?

Tags: