NULL Value in SQL statement

Source: Internet
Author: User

I wrote this code today.
Copy codeThe Code is as follows:
DECLARE @ recognition NVARCHAR (20)
SET @ ASD = NULL

IF (@ ASD = NULL)
BEGIN
PRINT 1
END
Originally, I wanted to print out 1. But no. Modify the Code as follows:

DECLARE @ recognition NVARCHAR (20)
SET @ ASD = NULL

IF (@ asd is null)
BEGIN
PRINT 1
END

In this way, 1 is printed correctly.
Then, modify the if statement
Copy codeThe Code is as follows:
IF (NULL = NULL)
BEGIN
PRINT 1
END

Result: 1 is not printed.
Therefore, we can sum up that the NULL value in the SQL statement is not equal to any value when performing the "=" operation. Use "is null" to determine whether the value is null.
We can also use the ISNULL function (@ str, 0) = 0 to determine whether the variable is NULL.
A small problem. record it, strengthen your memory, and be good at summing up.

========================================================== ==========================================================

Supplement: NULL and "+" operators.

"+" Can be used to connect strings and perform addition operations. What is the result of "+" between a NULL value and a string? I wrote the following verification program:
  Copy codeThe Code is as follows:

DECLARE @ str NVARCHAR (200)
DECLARE @ str1 NVARCHAR (200)
DECLARE @ str2 NVARCHAR (200)

SET @ str = NULL
SET @ str1 = 'You look pretty today'
SET @ str2 = @ str + @ str1

PRINT @ str2

The results are not printed. Why? Let's test what is @ str2 now?
IF (@ str2 is null)
PRINT 1
The result is 1, so we can guess that NULL and any type of "+" operations still return NULL values.
  
Write another "+" verification program for the NULL and INT types:
Copy codeThe Code is as follows:
DECLARE @ num INT
DECLARE @ num1 INT
DECLARE @ num2 INT

SET @ num = 1
SET @ num1 = 2
SET @ num2 = @ num + @ num1
PRINT @ num2

3 is printed, and the red bold part is replaced with SET @ num = NULL. What is the result? Try it by yourself, haha.

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.