At first, I thought that !=
was invalid T-SQL, but apparently !=
and <>
are identical. TIL.
Now, that said, NULL
is a major pain in the ass. NULL
is completely indeterminate - it’s not “equal to” anything, not even itself. Which also means that it’s not “not equal to” anything.
So if you want to exclude those NO COC
records but include the NULL
records, you need to do this:
WHERE (SC.Comment <> 'NO COC' or SC.Comment IS NULL)
I put it in parens in case you need to add other predicates to the WHERE
clause.
Now, for the more hardcore stuff.
Like I said above, NULL
is a pain in the ass, and the above construct could really wreak havoc with your execution plan for the query because it makes indexes less useful. I wish I could find the blog post I read about it now, but depending on other factors (you’ll quickly learn that the DBA’s favorite answer to anything is “it depends”), you may get much better performance by breaking it into two different queries. The important part here is that whatever
and wherever
here have to be identical, and any additional predicates in the WHERE
clause have to be copied & pasted as well.
SELECT whatever FROM whatever WHERE SC.Comment <> 'NO COC'
UNION
SELECT whatever FROM whatever WHERE SC.Comment IS NULL
YMMV, of course - you’ll want to try it both ways and look at the I/O statistics & execution plans to see if there’s a significant difference.