How to know which column is giving the value in Coalesce

Beantwortet How to know which column is giving the value in Coalesce

  • Freitag, 10. August 2012 13:36
     
     

    Hi

    I Have Coalesce(Col1,col2,col3) as result

    i want know which column is returning value in the output....

    Thank u in advance

Alle Antworten

  • Freitag, 10. August 2012 13:41
     
     Vorgeschlagene Antwort

    It will return the first NOT NULL column value as output. If you want to see what is coming exactly, you may add Col1, Col2 and Col3 also as part of SELECT column list along with COALESCE function.

    The function is written in the order of column priority that your requirement may have.

    Thanks!

  • Freitag, 10. August 2012 13:53
     
     Beantwortet

    You can also have one more column like this

    select Coalesce(Col1,col2,col3), case when Coalesce(Col1,col2,col3)=col1 then 'Col1'

    when Coalesce(Col1,col2,col3)=col2 then 'Col2'

    when Coalesce(Col1,col2,col3)=col3 then 'Col3'

    end+' is shown' extra_column

    from tab1;

    go


    Many Thanks & Best Regards, Hua Min