The case expression implementation returns a value based on conditional logic


In the development process, we often need to return a value based on conditional logic for a column. Then, we need to use case expression at this time.

For example, the following query on the products table uses a case expression in the select statement to generate information describing the value of the categoryid column.

The SQL code is as follows:

--Set database context
USE TSQLFundamentals2008;

SELECT productid,productname,categoryid,
CASE categoryid
WHEN 1 THEN 'Beverages'
WHEN 2 THEN 'Condiments'
WHEN 3 THEN 'Dairy Products'
WHEN 4 THEN 'Confections'
WHEN 5 THEN 'Grains/Cereals'
WHEN 6 THEN 'Meat/Poultry'
WHEN 7 THEN 'Produce'
WHEN 8 THEN 'Seafood'
ELSE 'Unkown Category'
END AS categoryname
FROM Production.Products;

Query results:

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]