Skip to content
Advertisement

Transform Columns to Rows dynamically using T-SQL

I have the following table where I need to transform Columns to Rows

tbl_Survey:

I need to have the following result:

To have this result I used the following code:

But, my Quest_1, Quest_2, Quest_3 values could potentially be changed / or even added a new once…

Are there ways to code them, so it can be used for any text values (not only for Quest_1 2 3)? Not necessary with the UNPIVOT… Could be any other way

Thank you

Advertisement

Answer

You need to make a dynamic UNPIVOT. Check the example below:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement