I’m confused by the result of the two SQL queries. Give the table content and two queries:
Col1 Status
---- ------
abcd "null"
abcd PENDING
defg "null"
defg PENDING
a) SELECT Col1, STATUS FROM TABLE_A WHERE KEY IN ('abcd','defg');
b) SELECT Col1, STATUS FROM TABLE_A WHERE KEY IN ('abcd','defg') and NOT (STATUS = 'PENDING')
result of (a) shows all 4 rows, and result of (b) shows nothing!
Column ‘Status’ can have values other than ‘PENDING’, and for reporting reason, I cannot use ‘STATUS IS NOT NULL’ as filter. I CANNOT understand why query (b) return nothing. Thanks in advance for any help
Platform: MSSQL 2008
SQL uses three valued logic (
True,False,Unknown). TheWHEREclause needs to evaluate toTruefor a row to be returned.STATUS = 'PENDING'returnsUNKNOWNwhenStatus IS NULL.If you negate
Unknownyou still getUnknown.See SQL and the Snare of Three-Valued Logic
You would need to rewrite the query as
(Though it is possible to use the deprecated
ANSI_NULLSsession option to get the behaviour you want)