In my table I need to search specific columns in a specific row to find which column contains the value I define.
For example in my table
ID | Col1 | Col2 | Col3 | Col4 | Col5 | Col6
--------------------------------------------
1 | A | B | C | C | B | A
2 | C | B | A | A | C | B
3 | B | A | C | B | A | C
I need a query to search columns 4, 5 and 6 in Row 2 and tell me in which column value B occurs (Col6).
Using a series of
CASEconditions to match'B'against each column will do the job. Each must supply the string literal name of the column as itsTHENvalue. You cannot dynamically determine the column names – they must be hard-coded.To return more than one, you can wrap the
CASEstatements in aCONCAT()that returns either the column name or an empty string. This means using several separateCASEstatements though, instead of multiple conditions for the same case.