Question : Get Columnname of a matched value

Hi,

I am trying to create a procedure that will lookup a certain number of columns that will match a value and return that column name, e.g.
columnA
columnB
columnC
columnD
columnE
columnF
columnG
columnH

if the value of @IDValue = 'Stuart'

it would it will check each column for that value if matched it will return the Columnname otherwise return 'no found', there will only be one match.

I thought about using a case but am not sure if this the best way to do this.

Thanks

Answer : Get Columnname of a matched value

Try with this query:


select
            case @IdValue
                  when ColumnA  then 'ColumnA'
                  when ColumnB  then 'ColumnB'
                  when ColumnC  then 'ColumnC'
                  when ColumnD  then 'ColumnD'
                  when ColumnE  then 'ColumnE'
                  when ColumnF  then 'ColumnF'
                  when ColumnG  then 'ColumnG'
                  when ColumnH  then 'ColumnH'
                  Else 'No Found'
            End                  
            from yourtable
Random Solutions  
 
programming4us programming4us