I have a table X
ID A B C D
1 T T F T
2 F T F T
3 T F T F
So if my input is 1 for ID, then I want all column names that have value T for row 1. In above case A,B,D or If ID is 3 then A and C.
How could I list these columns?
You can use UNPIVOT for this
SELECT Y
FROM Table1
UNPIVOT (X FOR Y IN ([A], [B], [C], [D])) U
WHERE [ID] = 1 AND X = 'T'
Returns
+---+
| Y |
+---+
| A |
| B |
| D |
+---+
SQL Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With