Skip to content
Advertisement

TSQL filter that I do not understand, 2 versions that I thought would return the same data do not

So I have a SQL statement that was not returning data like it should (MSSQL Server), in part of the query I had this

turns out if I changed that to

then it returned the data I expected, I would have thought those 2 things would be equivalent but obviously not in TSQL.

Here is the full Statement

My question is why do those return different data sets?

Advertisement

Answer

t.Invc_Sts_Cd <> 2 and (t.Invc_Sts_Cd <> 2 OR t.Invc_Sts_Cd IS NULL) are not equivalent, but they are similar.

The key thing you need to understand here is how NULL values work. Nothing is equal to or not equal to NULL, including NULL itself; NULL = NULL isn’t TRUE (though is isn’t FALSE either, but I come to that shortly). The only way to evaluate against a NULL is using IS NULL and IS NOT NULL.

When doing a comparison against NULL, if you don’t use IS (NOT) NULL then the result will be UNKNOWN. For a expression like WHERE Column = 1 then it isn’t a problem when Column has the value NULL, as it isn’t 1. For other expressions, it does.

For WHERE Column <> 2, when Column has the value NULL it gives result the UNKNOWN, which importantly is not TRUE. Thus the row does not meet to criteria.

The same is true for an expression like WHERE NOT(Column = 1). If Column has the value NULL, then Column = 1 = UNKNOWN and NOT(UNKNOWN) is still UNKNOWN.

Therefore, if you have NULL values, and are performiong comparisons which rely on them, you must include IS NULL and/or IS NOT NULL logic.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement