SQL Server compound query condition (and,or,not) method for handling null values

Source: Internet
Author: User

In SQL 3 value logic, a query condition can produce the following three scenarios: True,false,null. Only records that satisfy the value of the WHERE clause are true appear in the results table.

A null value affects the results of the query condition, and the result is subtle.

The following is the truth table for And,or,not in SQL.

Table 1 and Truth tables

TRUE

FALSE

Null

TRUE

TRUE

FALSE

Null

FALSE

FALSE

FALSE

FALSE

Null

Null

FALSE

Null

Table 2 or of the truth tables

TRUE

FALSE

Null

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

Null

Null

TRUE

Null

Null

Table 3 Truth tables for not

TRUE

FALSE

Null

FALSE

TRUE

Null

When more than two query conditions are combined with and, or, not, the precedence of not is the highest, followed by and, and finally, OR. Parentheses are best used to avoid ambiguity and to ensure portability.

A between B and C are equivalent to (a>=b ) and (a<=c), so the rules for handling null values in between clauses can be derived from the truth table.

Similarly, a in (b,c,d) is equivalent to (a=b)or(a=c)or(A=d), according to the truth table, as long as there is one of these three expressions is null, the result returned is definitely null.

Therefore, thebetween clause and the in clause do not increase the expressive power of the SQL statement.

There is a null value test in SQL, that is: the field is (not) null , but it returns only two cases: TRUE or false.

SQL Server compound query condition (and,or,not) method for handling null values

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.