How can I turn a column name into a result value in SQL Server?

Posted by Brennan on Stack Overflow See other posts from Stack Overflow or by Brennan
Published on 2010-03-22T20:54:58Z Indexed on 2010/03/22 21:01 UTC
Read the original article Hit count: 410

Filed under:
|
|

I have a table which has essentially boolean values in a legacy database. The column names are stored as string values in another table so I need to match the column names of one table to a string value in another table. I know there has to be a way to do this directly with SQL in SQL Server but it is beyond me.

My initial thought was to use PIVOT but it is not enabled by default and enabling it would likely be a difficult process with pushing that change to the Production database. I would prefer to use what is enabled by default.

I am considering using COALESCE to translate the boolean value to the string that value that I need. This will be a manual process.

I think I will also use a table variable to insert the results of the first query into that variable and use those results to do the second query. I still have the problem that the columns are on a single row so I wish I could easily pivot the values to put the column names in the result set as strings. But if I could easily do that I could easily write the query with a sub-select.

Any tips are welcome.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005