Unanswered: In Select with case Getting the actual column Name
I have a table with three columns.
I am selecting one column from it on some condition using case.
i.e select case when col1 is not NULL then col1
when col2 is not Null then col2
Now i want to know which column is selected. The above query will give the column heading as case or if I give any alias then the column heading will be alias name where their is no way to differentiate or to know which column is selected.
Or their is any way to get the original column name which is selected.
You cannot use a parameter that has column names as its values. You have to assign values manually. For example, write the following code immediately after the first case expression and before the FROM clause:
, CASE WHEN col1 IS NOT NULL THEN 'col1'
WHEN col2 IS NOT NULL THEN 'col2'
ELSE 'col3' END selected_col