Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to get column name from a column value

Author  Topic 

Sathiyaseelan.ts
Starting Member

1 Post

Posted - 2011-09-24 : 09:29:38
hi all,

how to get the column name for a particular column value.
example:

create table test(id number,col2 varchar,col3 varchar);

insert into test values(1,'true','false');
insert into test values(2,'false','true');
commit;

i want to select column name for data 'true' having id=1;
i.e) i want to col2 as output;

sathiyaseelan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-24 : 09:33:34
one way to do it is unpivot

select columnname
from test
unpivot(val for columnname in (col2,col3))u
where val = 'true'
and id=1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -