Getting column names based on value

Posted on 2014-09-02
Last Modified: 2014-09-03
Given a set number columns with values True or False, what is the most efficient way of getting the columns with value TRUE. e.g. select column_name from table where column_name.value='TRUE'

I have the query below which works, but I am trying to see if there is a more efficient/condensed/less lines of code way to do it?

Declare @col varchar(255), @cmd varchar(max)
DECLARE getinfo cursor for
SELECT FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID and IN ('Maths','English','History','General Science')
WHERE t.Name = 'classes'

OPEN getinfo

FETCH NEXT FROM getinfo into @col


    SELECT @cmd = 'IF EXISTS (SELECT * FROM classes WHERE ([' + @col + '] =''True'' ) and id=3) BEGIN print ''' + @col + ''' end'

    FETCH NEXT FROM getinfo into @col

CLOSE getinfo

Open in new window

Question by:takwirirar
    1 Comment
    LVL 44

    Accepted Solution

    Maybe this solution:
    SELECT CASE Maths 
                       WHEN ''True'' THEN 'Maths'
                       ELSE NULL
                  CASE English
                       WHEN ''True'' THEN 'English'
                       ELSE NULL
                 CASE History
                       WHEN ''True'' THEN 'History'
                       ELSE NULL
               CASE [General Science]
                       WHEN ''True'' THEN 'General Science'
                       ELSE NULL
    FROM classes 
    WHERE id=3

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
    SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now