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_NULL
s concern only queries like this one (where the WHERE
includes the specific word NULL
)?
SELECT Region
FROM employees
WHERE Region = NULL
Set ANSI NULLS OFF will make NULL = NULL comparision return true. EG :
will return some result as displayed below: zcwInvoiceDeliveryType 744547 NULL zcExpenseRptStatusTrack 2099048 NULL ZCVendorPermissions 2840564 NULL ZCWOrgLevelClientFee 4322525 NULL
While this query will not return any results:
If ANSI_NULLS is set to "ON" and if we apply = , <> on NULL column value while writing select statement then it will not return any result .
Example
create table #tempTable (sn int, ename varchar(50))
insert into #tempTable
select 1, 'Manoj'
UNION ALL
select 2, 'Pankaj'
UNION ALL
select 3, NULL
UNION ALL
select 4, 'Lokesh'
UNION ALL
select 5, 'Gopal'
SET ANSI_NULLS ON
select * from #tempTable where ename is NULL -- (1 row(s) affected)
select * from #tempTable where ename = NULL -- (0 row(s) affected)
select * from #tempTable where ename <> NULL -- (0 row(s) affected)
SET ANSI_NULLS OFF
select * from #tempTable where ename is NULL -- (1 row(s) affected)
select * from #tempTable where ename = NULL -- (1 row(s) affected)
select * from #tempTable where ename is not NULL -- (4 row(s) affected)
select * from #tempTable where ename <> NULL -- (4 row(s) affected)
https://docs.microsoft.com/en-us/sql/t-sql/statements/set-ansi-nulls-transact-sql
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 nonnull values in column_name.
For e.g
SET ANSI_NULLS ON
IT Returns all values including null values in the table
SET ANSI_NULLS off
it Ends when columns contains null values
It means that no rows will be returned if
@region
isNULL
, when used in your first example, even if there are rows in the table whereRegion
isNULL
.When
ANSI_NULLS
is 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 isNULL
produces the third logic value -UNKNOWN
(as opposed toTRUE
andFALSE
).UNKNOWN
values propagate through any combining boolean operators if they're not already decided (e.g.AND
with aFALSE
operand orOR
with aTRUE
operand) or negations (NOT
).The
WHERE
clause is used to filter the result set produced by theFROM
clause, such that the overall value of theWHERE
clause must beTRUE
for the row to not be filtered out. So, if anUNKNOWN
is 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
NULL
columns 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,
NULL
does 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 OFF
was the setting that had no affect.If
@Region
is not anull
value (lets say@Region = 'South'
) it will not return rows where the Region field is null, regardless of the value of ANSI_NULLS.ANSI_NULLS will only make a difference when the value of
@Region
isnull
, i.e. when your first query essentially becomes the second one.In that case, ANSI_NULLS ON will not return any rows (because
null = null
will yield an unknown boolean value (a.k.a.null
)) and ANSI_NULLS OFF will return any rows where the Region field is null (becausenull = null
will yieldtrue
)