4

Why does this give me 1 which is what I was expecting:

IF (SELECT 123) = 123
    PRINT 1
ELSE
    PRINT 2

But this gives me 2 which I was not expecting:

IF (SELECT NULL) = NULL
    PRINT 1
ELSE
    PRINT 2

4 Answers 4

3

NULL values are checked by IS NULL

you have to use:

IF (SELECT NULL) IS NULL
    PRINT 1
ELSE
    PRINT 2

from the manual:

To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression

Sign up to request clarification or add additional context in comments.

Comments

3

If you put NULLS OFF

SET ANSI_NULLS OFF
    IF (SELECT NULL) =  NULL
        PRINT 1
    ELSE
        PRINT 2

then you will get PRINT 1

Comments

1

You cant check NULL with =. For that IS has to be used. Ex:

IF (SELECT NULL) IS NULL
    PRINT 1
ELSE
    PRINT 2

Comments

1

You cannot check NULL using =. You need to use IS NULL like the following

IF (SELECT NULL) IS NULL
    PRINT 1
ELSE
    PRINT 2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.