T_sql column-based logical expression (case)

Source: Internet
Author: User

This digest from: http://www.cnblogs.com/kissdodog/p/3154371.html (thanks to the author for sharing, summed up very well)

A column-based logical expression is actually a case expression. Can be used after Select,update,delete,set and in,where,order by and having clauses. Since this is a T-SQL query, only the case expression is used in the SELECT clause and the ORDER BY clause.

The action of the case expression with the IF in the programming language ... Then ... else logic is similar. Just a case expression does not control the flow of a T-SQL program in T-SQL, but only as a column-based logical use.

The first simple way to use:

SELECT id,name, Case    sex when     0 Then ' Male ' when    1 Then ' woman '     ELSE ' unclear '     END as gender from person

The actual case expression can be divided into two types:

Case Simple expression: compares an expression to a set of simple expressions to determine the result.

SELECT id,name,    gender = case Sex--the difference is just putting the alias here. When    0 Then ' Male ' when    1 Then ' Women '     ELSE ' unclear '     endfrom Person

Because the value of the case expression is confined to only one column, the value data type after then must be the same or compatible, otherwise an error will be given.

In the above statement, there is also an optional "else" statement, which can be omitted, but the best practice is to keep else, otherwise all values that are not within the range of the matched value will be "NULL".

Case search expression (case searched expression): Computes a set of Boolean expressions to determine the result.

Unlike case simple expressions, case search expressions provide more powerful functionality, and case search expressions not only use more complex logical expressions, but can also determine the values of displayed columns based on the data of multiple columns.

SELECT id,name,    gender = case--notice there's nothing behind this case. When    sex=0 then ' handsome '--of course sex you can switch to any other column when    sex =1 Then ' Beauty '    ELSE ' not clear '     endfrom person

  Multiple column combinations to determine:

SELECT Id,name,    case    If id = 3 and sex = 0 Then ' handsome guy '--here to note the order, precedence shows the previous match, this later will refer to when    id = 4 and sex = 1 T HEN ' Belle ' when     sex = 0 Then ' male ' when    sex = 1 Then ' woman '    ELSE ' not clear '    END as gender from person

  

SELECT Id,name, case when score > "excellent" When score > "good" when Score > "Medium" When Sco Re > Then ' Pass ' ELSE ' fail ' END as score from score

  

The case expression can classify the sorted result in order by, so that it can be in ascending order according to certain conditions, or descending in accordance with some other condition, but in general, the following is an example.

Or just that table, I think, when the gender for the male ID descending sort, if the gender is the female ID ascending sort. The SQL statements are as follows:

SELECT Id,name,sex from man order by case when     sex=0 then Id END DESC,    --gender is male, Id descending case when    sex=1 TH EN ID END ASC      --sex for female, Id ascending

  

T_sql column-based logical expression (case)

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.