The information in this article applies to:
SYMPTOMSQueries executed with ANSI_NULLS OFF may return varying or unintuitive results depending on the situation and SQL Server version. This article explicitly defines the expected behavior for all types of comparisons with SQL Server 6.x and SQL Server 7.0 with ANSI_NULLS OFF. WORKAROUND
Always use ANSI_NULLS ON and use the IS NULL and IS NOT NULL syntax when doing comparisons with NULL values. ANSI_NULLS OFF behavior is included in SQL Server 7.0 only for backwards compatibility.
If you want to return all the rows where b <> 2, and also want to include rows where b is NULL in the result set, use the following query:
MORE INFORMATION
SQL Server 7.0 Books Online states the following in the SET ANSI_NULLS (TSQL) topic:
The SQL-92 standard requires that an equals (=) or not equal to (<>) comparison against a null value evaluates to FALSE. When SET ANSI_NULLS is ON, a SELECT statement using WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement using WHERE column_name <> NULL returns zero rows even if there are nonnull values in column_name. The nuances of the ANSI_NULLS OFF behavior are not explicitly detailed in the above section. In almost all cases, SQL Server 7.0 behavior with ANSI_NULLS OFF is the same as SQL Server 6.x. The one instance where SQL Server 6.x and 7.0 behave differently is when evaluating <> with variable expressions that evaluate to NULL. For example, in SQL Server 6.x, the query below would not return rows where the price column is NULL (it returns 16 rows total). In SQL Server 7.0, it returns the same rows as 6.x, plus the rows where the price column is NULL (18 rows total).
The tables below define the expected behavior for SQL Server 6.x and 7.0 with comparisons using ANSI_NULLS OFF behavior. The column heading abbreviations are described in the following key: Tables Key:
SQL Server 7.0 with = comparison and ANSI_NULLS OFF
SQL Server 7.0 with <> comparison and ANSI_NULLS OFF
SQL Server 6.x with = comparison and ANSI_NULLS OFF
SQL Server 6.x with <> comparison and ANSI_NULLS OFF
Additional query words: ODBC OLEDB OLE DB DBLIB ANSI_DEFAULTS resultset non-null
Keywords : |
Last Reviewed: February 23, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |