Monday, March 19, 2012

Case / Switch function help

Starting to play around with SQL server at work and this is my question:

In the query design mode in access I can make one of the fields an expression that is driven by a built-in switch function.
i.e. Switch([CategoryName] Like "Beverage","Drink",[CategoryName] Like "Cheese","Dairy")
This results in the additional column field I created to display "Drink" for each record that has the CategoryName = "Beverage", and "Dairy" for "Cheese".

Can I do something like this in SQL server in the view designer itself, or do I need to make a user defined function and call it?

Thanks in advance for any help.Use the CASE statement in SQL Server.|||Can I do this in the view design mode or do I need to make a user defined function?
Any sample code would be really appreciated.|||CASE WHEN [CategoryName] = 'Beverage' THEN 'Drink'
WHEN [CategoryName] = 'Cheese' THEN 'Dairy'
ELSE 'Unknown'
END AS DerivedColumnName

No comments:

Post a Comment