I need to update the next column to the column that contains an specific number.
The ff are columns in the table C.
C1_K | C1_ID | C2_K | C2_ID | C3_K | C3_ID
Logic:
- Look for where number 5 is found is it in C1_K OR C2_K OR C3_K.
- If it is in C1_K then select the text value C1_ID.
- If it is in C2_K then select the text value C2_ID.
- If it is in C3_K then select the text value C3_ID.
Please advice. I need to update the column next to the matching K column. Thanks.
Note:
- All C*_K columns contains unique numbers.
- All C*_ID columns contains unique text. (these are names)
Sample Data:
154 | NULL | 155 | SantoRyUu | 156 | NULL
If 155 is chosen SantoRyUu will be updated.
(Design problems aside.)
You have to mention the columns by name, since SQL Server doesn’t have syntax for “next to.” You may be able to do this with dynamic SQL, but yuck. Here is one possibility:
Results: