[null, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/HTPictures/635649192247078758.JPG", "src": "https://techfunda.com/HTPictures/635649192247078758.JPG"}, null, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/HTPictures/635649193158982629.JPG", "src": "https://techfunda.com/HTPictures/635649193158982629.JPG"}, null, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/HTPictures/635649214098319204.JPG", "src": "https://techfunda.com/HTPictures/635649214098319204.JPG"}, null, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/HTPictures/635649201361746123.jpg", "src": "https://techfunda.com/HTPictures/635649201361746123.jpg"}, null, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/facebook.png", "src": "https://techfunda.com/images/social/facebook.png", "formatted_filename": "facebook", "alt_text": "Facebook"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/twitter.png", "src": "https://techfunda.com/images/social/twitter.png", "formatted_filename": "twitter", "alt_text": "Twitter"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/linkedin.png", "src": "https://techfunda.com/images/social/linkedin.png", "formatted_filename": "linkedin", "alt_text": "LinkedIn"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/google-plus.png", "src": "https://techfunda.com/images/social/google-plus.png", "formatted_filename": "google plus", "alt_text": "GooglePlus"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/stumbleupon.png", "src": "https://techfunda.com/images/social/stumbleupon.png", "formatted_filename": "stumbleupon", "alt_text": "StumbleUpon"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/pinterest.png", "src": "https://techfunda.com/images/social/pinterest.png", "formatted_filename": "pinterest", "alt_text": "Pinterest"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/delicious_32.png", "src": "https://techfunda.com/images/social/delicious_32.png", "formatted_filename": "delicious", "alt_text": "Delicious"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/reddit_32.png", "src": "https://techfunda.com/images/social/reddit_32.png", "formatted_filename": "reddit", "alt_text": "Reddit"}, {"document_url": "http://techfunda.com/howto/152/convert-rows-into-columns", "unformatted_src": "/images/social/email.png", "src": "https://techfunda.com/images/social/email.png", "formatted_filename": "email", "alt_text": "Email"}, null, null]
SQL Server > Query






















Convert rows into columns in SQL Server

How to convert rows into columns of the database tables in SQL Server?


Many a times, our database table structure are not in proper ways where our data are stored into rows and columns format where rows are only data.

Look at this database table design. In PersonalDetailsData table, we are storing specific attributes of a person into DataName rows and its value in DataValue rows. To identify the person for which these data are stored, we have a PersonalDetailsId column that is the foreign key of primary table PersonalDetails.

Using SELECT record from database table gives data in above format that is very tough to present and understand to the end user.

The solution of this problem is to convert the unique DataName values into columns and give its values as DataValue in each rows like bellow To do this, we need to use PIVOT clause like below. In the above query, we are selecting the Height, [Weight], Eyes and PersonalDetailsId from subquery formed with the help of PARTITION BY clause.

The first set of selected statement adds a columns starting with ‘param…’ by partitioning the columns of the PersonalDetailsData that has alias PD.

Now the next set of highlighted statement select the DataValue for each DataName written into PersonalDetailsData table.

All this is shown in below picture. The first result is the raw data of PersonaDetailsData table and the 2nd result set is converted data.

The second result set is easy to understand and present to the end user. Views: 5663 | Post Order: 59