• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

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
0
sanjshah12
Asked:
sanjshah12
  • 2
  • 2
1 Solution
 
felipevidaurriCommented:
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
0
 
SharathData EngineerCommented:
<< 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' >>

Do you want to search for 'Stuart' in all your columns, Column A to H?
0
 
SharathData EngineerCommented:
Can you explain little bit more?
0
 
sanjshah12Author Commented:
Thanks felipevidaurri solution works great!
0
 
felipevidaurriCommented:
Excellent!.., your welcome...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now