Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dispaly column name

Status
Not open for further replies.

mytaurus2000

Technical User
Oct 4, 2006
138
0
0
US
Is there a SQL query that allows you to query a table and return the column name if the value of the column is true?
 
I am unable to fully grasp what you want.

Do you want to check to see if a column exists?

If the datatype of the column is a certain type?

Do you want the data out of the column if the data = some value?

-Sometimes the answer to your question is the hack that works
 
I'm sorry, I have a table with several columns of boolean datatype, true/false. I need a query that will display the column name when it's value is true.

column1 column2 column3

true false false

I would like to have column 1 showing as a result of the query.
 
Select tableID,
Case when column1 = 1 then 'Column1' else '' end as Column1,
Case when column2 = 1 then 'Column2' else '' end as Column2,
Case when column3 = 1 then 'Column3' else '' end as Column3,
From myTable

or

Select tableID,
Case
when column1 = 1 then 'Column1'
when column2 = 1 then 'Column2'
when column3 = 1 then 'Column3' else '' end as Column
From myTable

Is that what you want? By the way, the true false is a bit, not a boolean in SQL


-Sometimes the answer to your question is the hack that works
 
try

Code:
Case 1 
 when column1 then column1           
 when column2 then column2
 when column3 then column3
end
 
sorry sb


Case 1
when column1 then 'column1'
when column2 then 'column2'
when column3 then 'column3'
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top