The definition says:
When SET ANSI_NULLS is ON, a SELECT statement that uses WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement that uses WHERE column_name <> NULL returns zero rows even if there are non-null values in column_name.
Does this mean that no nulls will be included in this query?
SELECT Region
FROM employees
WHERE Region = @region
Or do ANSI_NULLs concern only queries like this one (where the WHERE includes the specific word NULL)?
SELECT Region
FROM employees
WHERE Region = NULL
It means that no rows will be returned if
@regionisNULL, when used in your first example, even if there are rows in the table whereRegionisNULL.When
ANSI_NULLSis on (which you should always set on anyway, since the option to not have it on is going to be removed in the future), any comparison operation where (at least) one of the operands isNULLproduces the third logic value –UNKNOWN(as opposed toTRUEandFALSE).UNKNOWNvalues propagate through any combining boolean operators if they’re not already decided (e.g.ANDwith aFALSEoperand orORwith aTRUEoperand) or negations (NOT).The
WHEREclause is used to filter the result set produced by theFROMclause, such that the overall value of theWHEREclause must beTRUEfor the row to not be filtered out. So, if anUNKNOWNis produced by any comparison, it will cause the row to be filtered out.@user1227804’s answer includes this quote:
from
SET ANSI_NULLS*However, I’m not sure what point it’s trying to make, since if two
NULLcolumns are compared (e.g. in aJOIN), the comparison still fails:The above query returns 0 rows, whereas:
Returns one row. So even when both operands are columns,
NULLdoes not equalNULL. And the documentation for=doesn’t have anything to say about the operands:However, both 1 and 2 are incorrect – the result of both comparisons is
UNKNOWN.*The cryptic meaning of this text was finally discovered years later. What it actually means is that, for those comparisons, the setting has no effect and it always acts as if the setting were ON. Would have been clearer if it had stated that
SET ANSI_NULLS OFFwas the setting that had no effect.