4

This is obviously not going to return a row...

select 1 where null = ''

But why does this also not return a row?

select 1 where null <> ''

How can both of those WHEREs be "false"?

0

8 Answers 8

5

"How can both of those WHEREs be "false"?"

It's not! The answer is not "true" either! The answer is "we don't know".

Think of NULL as a value you don't know yet.

Would you bet it's '' ?

Would you bet it's not '' ?

So, safer is to declare you don't know yet. The answer to both questions, therefore, is not false but I don't know, e.g. NULL in SQL.

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

Comments

3

Because this is an instance of SQL Server conforming to ANSI SQL ;-)

NULL in SQL is somewhat similar to IEEE NaN in comparison rules: NaN != NaN and NaN == NaN are both false. It takes a special operator IS NULL in SQL (or "IsNaN" for IEEE FP) to detect these special values. (There are actually multiple ways to detect these special values: IS NULL/"IsNaN" are just clean and simple methods.)

However, NULL = x goes one step further: the result of NULL =/<> x is not false. Rather, the result of the expression is itself NULL UNKNOWN. So NOT(NULL = '') is also NULL UNKNOWN (or "false" in a where context -- see comment). Welcome to the world of SQL tri-state logic ;-)

Since the question is about SQL Server, then for completeness: If running run with "SET ANSI_NULLS OFF" -- but see remarks/warnings at top -- then the "original" TSQL behavior can be attained.

"Original" behavior (this is deprecated):

SET ANSI_NULLS OFF;
select 'eq' where null = '';       -- no output
select 'ne' where null <> '';      -- output: ne
select 'not' where not(null = ''); -- output: not; null = '' -> False, not(False) -> True

ANSI-NULLs behavior (default in anything recent, please use):

SET ANSI_NULLS ON;
select 'eq' where null = '';       -- no output
select 'ne' where null <> '';      -- no output
select 'not' where not(null = ''); -- no output; null = '' -> Unknown, not(Unknown) -> Unknown

Happy coding.

2 Comments

NULL = x evaluates to UNKNOWN. In a WHERE context, the effect is the row is removed from the resultset. In a constraint context, the effect is the update to the row does not fail.
@onedaywhen Thanks for the corrections. I have updated the answer appropriately.
2

Null has some very odd behaviour when comparing - the wikipedia article explains it quite well. In a nutshell, as well as true and false, there is an unknown value, which SQL returns when doing a comparison.

Comments

1

The SQL standard specifies that NULL = x is false for all x (even if x is itself NULL) and SQL Server is just following the standard. If you want to check if something is or is not NULL, then you have to use x IS NULL or x IS NOT NULL.

Comments

1

Databses have what is known as three valued logic. The values of true, false, and unknown.

Read this http://www.simple-talk.com/sql/learn-sql-server/sql-and-the-snare-of-three-valued-logic/

Comments

0

Because any comparison with NULL returns false (or to be precise: returns NULL)

As NULL is the absence of information you cannot tell whether is equal to or not equal to something.

6 Comments

I think it's odd, I assumed that (the absence of information) != (information)
@jayrdub: think of it this way: if you don't own a spaceship, how can you tell which color that spaceship has? It's neither white nor is it not white.
But I do own a spaceship. Thanks, that helps.
To be precise, any equality comparison with NULL evaluates to UNKNOWN.
@onedaywhen: Interesting. What's the difference between NULL and UNKNOWN?
|
0

Probably getting a little repetitive here, but my two cents anyway:

A. a_horse_with_no_name's example (comment, above) is a very good one!

B. In non=mathmatical terms, NULL is an unknown value. An empty String is a string with a length of zero - hence, a "known" value. This is why NULL does not compare equally or not equally to an empty string.

C. Since NULL represents unknown, it is impossible to compare two NULL values for equality. If you don't know the value of X, and you don't know the value of Y, then you don't know if they are equal or not.

Comments

0

See SQL-92 8.2 comparison predicate saying:

General Rules

  1. Let X and Y be any two corresponding <row value constructor element>s. Let XV and YV be the values represented by X and Y, respectively.

    Case:

    a) If XV or YV is the null value, then "X <comp op> Y" is unknown.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.