Check 3 columns to return single value

  • In a query I'm working on I have three boolean columns to identify the type of record - only one out of the three can be true.

    e.g.

    IsPage bool

    IsFolder bool

    IsMenu bool

    In a query is it possible to have a query where one of the columns is something like ObjectType which is populated with either Page, Folder, Menu depending on which one of the above is set as True?

    Cheers,

    Julian

  • There's probably a simpler way, but this will work

    SELECT CASE IsPage WHEN 1 THEN 'Page'

     ELSE CASE IsFolder WHEN 1 THEN 'Folder'

       ELSE CASE ISMenu WHEN 1 THEN 'Menu'

        ELSE 'Unknown'

       END

      END

     END AS ObjectType

    FROM SomeObjectTable

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Many thanks, I didn't realise I could link the CASE statements together like that.

  • The same without nesting

    CASE

    WHEN IsPage=1 THEN 'Page'

    WHEN IsFolder=1 THEN 'Folder'

    WHEN ISMenu=1 THEN 'Menu'

    ELSE 'Unknown'

    END

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply