I’ve had a hunt round here and google and been un able to find anything.
I did come accross the CONCAT function, but as my company runs SQL Server 2008 thats not an option 🙁
I have a query that outputs multiple columns / rows and I want to disply the results all in one column.
Example:
SELECT client, owner, payer, status
FROM Main
WHERE status = 'Active'
Result:
client | owner | payer | status
1 | 2 | 3 | Active
10 | 11 | 12 | Active
What I Want it to look like
ColumnX |
1
2
3
Active
10
11
12
Active
Thanks in advance for your help and I hope that makes sense 🙂
Cheers,
Michael
What you are referring to is known as an
UNPIVOT. This takes the columns and converts them to rows:See SQL Fiddle with Demo
The
UNPIVOTwill give you the list of values in one column and then the column it came from in the other.The thing that is required with an
UNPIVOTis that all fields be of the same datatype, so you will need to perform a conversion of the datatypes so they can be presented in the same column.You could also use a
UNION ALLto perform this operation but again you will need tocast()the datatypes to be the same:See SQL Fiddle with Demo