How to use case when syntax in SQL

Source: Internet
Author: User
Tags copy end getdate sql range
No, take the case to replace it.

For example, the following statement shows the Chinese month
Copy CodeThe code is as follows:
Select GETDATE () as date, Case month (GETDATE ())
When one then ' 11 '
When then ' 12 '
else substring (' 1,234,567,890 ', Month (getdate ()), 1)
End+ ' Month ' as month

The case may be one of the most misused keywords in SQL. Although you may have used this keyword before to create a field, it has more usage. For example, you can use the case in the WHERE clause.

First let's take a look at the syntax of the case. In a general SELECT, the syntax is as follows:
Copy CodeThe code is as follows:
SELECT <myColumnSpec> =
Case
When <A> THEN <somethingA>
When <B> THEN <somethingB>
ELSE <somethingE>
End

In the above code, you need to replace the contents of the angle brackets with specific parameters. The following is a simple example:
Copy CodeThe code is as follows:
Use pubs
Go
SELECT
Title,
' Price Range ' =
Case
When Price is NULL THEN ' unpriced '
When Price < THEN ' Bargain '
When Price BETWEEN and THEN ' Average '
ELSE ' Gift to impress relatives '
End
From titles
ORDER BY Price
Go

This is a typical use of case, but there are more things you can do with it. For example, the following case in the GROUP by clause:
Copy CodeThe code is as follows:
SELECT ' number of Titles ', Count (*)
From titles
GROUP by
Case
When Price is NULL THEN ' unpriced '
When Price < THEN ' Bargain '
When Price BETWEEN and THEN ' Average '
ELSE ' Gift to impress relatives '
End
Go

You can even combine these options and add an ORDER BY clause as follows:
Copy CodeThe code is as follows:
Use pubs
Go
SELECT
Case
When Price is NULL THEN ' unpriced '
When Price < THEN ' Bargain '
When Price BETWEEN and THEN ' Average '
ELSE ' Gift to impress relatives '
End as Range,
Title
From titles
GROUP by
Case
When Price is NULL THEN ' unpriced '
When Price < THEN ' Bargain '
When Price BETWEEN and THEN ' Average '
ELSE ' Gift to impress relatives '
End,
Title
ORDER BY
Case
When Price is NULL THEN ' unpriced '
When Price < THEN ' Bargain '
When Price BETWEEN and THEN ' Average '
ELSE ' Gift to impress relatives '
End,
Title
Go

Note that in order to use case in the group by block, the query statement needs to repeat the case block in the SELECT block in the group by block.

In addition to selecting custom fields, cases are useful in many cases. In a further step, you can also get a set of sorted results that you previously thought would be impossible.

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.