The case expression implementation is based on conditional logic to return a value _mssql

Source: Internet
Author: User

In our development process, we often need to return a value for a column, based on conditional logic, then we need to use the case expression.

For example, the following query to the Products table uses a CASE expression in the SELECT statement to generate information that describes the value of the CategoryID column.

The SQL code is as follows:

--Setting the database context use
TSQLFundamentals2008;
Go

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 TH EN ' Meat/poultry ' when
7 THEN ' produce ' if
8 THEN ' seafood '
ELSE ' unkown Category ' end as
Categoryn Ame from
production.products;

Query results:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.