Two forms of case clauses in SQL

Source: Internet
Author: User

Case clause, which can be logically judged after a select.

Two forms: judging equality, judging unequal

The syntax for judging equality:

Case Column Name

When ... then ...

When ... then ...

else ...

End as column alias

One: Create a new table and insert the initial data.

--Check the entire table .Select* fromSdetail--Insert initial data insert into Sdetail (Sname,sscore,sgender) VALUES ('Andy Lau', About,0) insert into Sdetail (Sname,sscore,sgender) VALUES ('Louis Koo', -,0) insert into Sdetail (Sname,sscore,sgender) VALUES ('-Mei', -,1) insert INTO Sdetail E (sname,sscore,sgender) VALUES ('Liang', +,0) insert into Sdetail (Sname,sscore,sgender) VALUES ('Wang Leehom', About,0) insert into Sdetail (Sname,sscore,sgender) VALUES ('Liu Yifei', the,1)
View Code

Case clauses determine equality

- - Two forms of case syntax Select * 0' male ' 1 ' female '  as ' Sex '  from Sdetail
View Code

Execution Result:

Second, Judgment unequal (range)

A logical expression that contains column names then ...

When ... then ...

else ...

End as column alias

- case judgment Unequal (range)Select * , case when sscore>  Pass 'else' less than grid 'as'  '  from Sdetail
View Code

Execution Result:

Two forms of case clauses in SQL

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.