Comparing a value to a NULL in t-SQL

Viewed 8732

I was curious if it's legal in t-SQL to compare a NULL to a value?

For instance, if I have:

WITH ctx AS(SELECT 123 AS n0, NULL AS n1)
SELECT n0 FROM ctx
WHERE ctx.n1 < 130

the WHERE clause in that case is always evaluated as FALSE. Is it something I can rely on?

5 Answers

I've always used the EXISTS keyword along with EXCEPT like so

SELECT 1
WHERE EXISTS ((SELECT 1) EXCEPT (SELECT NULL))
Related