When case when encounters an empty string to 0, case empty string

Source: Internet
Author: User

When case when encounters an empty string to 0, case empty string
Note: If the field is of the varchar type, quotation marks are required for the condition after when.

SELECT (CASE 'marate_status' WHEN 0 then' married 'when 1 then' unmarried 'else' weizhi' END) AS marital_status FROM tj_archive WHERE id = 'd1407280006'

In the preceding statement, if marital_status is a null string instead of null, the query result is "married"

Finally, the correct result is:

SELECT (CASE 'marital _ status' WHEN '0' then' married 'when' 1 'then' unassociated 'else' weizhi' END) AS marital_status FROM tj_archive WHERE id = 'd1407280006'


The red part is a different sentence.


Why does the SQL statement use CASE WHEN to convert columns into NULL values?

This is because statistics are made by men and women separately.
Select -- avg (total score), avg (case when gender = 'male' then total score else 0 END) AS 'average score of boys ', avg (case when gender = 'femal' then total else 0 END) AS 'average female score 'from students -- group by gender -- remove this group by try


How can I put case when in where?

It cannot be placed in the where clause. For example, it is easier to use a subquery to query the select * from where field in (select case when field> 1 then 1 else 0 end from table)

Related Article

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.