Introduction to T-SQL IsNull functions

Source: Internet
Author: User

Today in the transfer of data to colleagues, colleagues feedback said data is small, I carefully check, found that the SQL statement conditions are correct, the logic is not right, finally after careful investigation, only to find the problem in the null field

There is a column in the table that is allowed as a null value, such as a record that queries a merchant name that is not a test name

My query for name<> ' test ' is 10 lines,

SELECT * FROM Test name<> ' test '

Check the data name= ' test ' has 90 rows, total record 120 rows, 20 rows do not meet <> ' test ', nor = ' test ', these 20 rows of record name are null

As shown, it is clear from a glance:

In the data table row Records, the data column often has a record of NULL, in fact, the null value represents the unknown, that is, empty, indicating nothing,

But it's not what we're talking about, it's not about the value 0, it's about the unknown data. A point character that can understand unknown data

Null values are handled differently than other values

Null cannot participate in the comparison because it is an unknown value, so it cannot be used after the comparison operator.

Name<>null,name=null, are not correct,

A null value cannot be compared to an operator, so how can the query column value be null?

We must use the is null and is not NULL operator,

If you query a user whose name is null

-- record is null SELECT COUNT (1fromWHEREisNULL

Query the user record whose name is not equal to ' aaaa ':

-- the right name is not equal to ' aaaa ' SELECT *  from WHERE ISNULL (U.name,") <> ' AAAA '

Actually, the IsNull function has been used here.

The IsNull function has two parameters, the first argument is the expression to evaluate, and the second is the substitution value when the first expression value is empty.

Introduction to T-SQL IsNull functions

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.