February 28, 2006 at 4:29 am
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
February 28, 2006 at 4:33 am
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
February 28, 2006 at 4:56 am
Many thanks, I didn't realise I could link the CASE statements together like that.
February 28, 2006 at 6:39 am
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