Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2012

    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
    else col3
    from table_name;

    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.

    Please guide me..

  2. #2
    Join Date
    Mar 2006
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts