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
 Return column name based on criteria?

Author  Topic 

jsamples1277
Starting Member

1 Post

Posted - 2012-01-26 : 09:31:45
Let's say I have a table, "myTable", with columns "col_1", "col_2", "col_3", "col_4" and respective values of "year", "2010", "2011", "2012". Now I want to write a select statement that will return a particular column name depending on where a particular value is located (ie. col_1 = 'year' and '2011' in (col_2,col_3,col_4). How would I accomplish this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 10:49:09
you mean you need to return header of column?

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-26 : 10:51:46
maybe this?

SELECT col
FROM
(
SELECT *
FROM table t
UNPIVOT(val FOR col IN (col_2,col_3,col_4))u
)m
WHERE val='2011'
AND col_1='year'


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

Go to Top of Page
   

- Advertisement -