You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Specifies ISO compliant behavior of the Equals (`=`) and Not Equal To (`<>`) comparison operators when they are used with null values in [!INCLUDE [ssnoversion](../../includes/ssnoversion-md.md)].
31
+
-`SET ANSI_NULLS ON` - Evaluates both `{expression} = NULL` and `{expression} <> NULL` as `False` if the value of `{expression}` is `NULL`. This is the ANSI-compliant behavior.
32
+
-`SET ANSI_NULLS OFF` - Evaluates `{expression} = NULL` as `True` and `{expression} <> NULL` as `False` if the value of `{expression}` is `NULL`. This is not a recommended behavior, because the `NULL` values should not be compared using `=` and `<>` operators.
30
33
31
34
> [!NOTE]
32
35
> `SET ANSI_NULLS OFF` and the ANSI_NULLS OFF database option are deprecated. Starting with [!INCLUDE [_ss2017](../../includes/sssql17-md.md)], ANSI_NULLS is always set to ON. Deprecated features shouldn't be used in new applications. For more information, see [Deprecated Database Engine features in SQL Server 2017](../../database-engine/deprecated-database-engine-features-in-sql-server-2017.md#transact-sql-1).
@@ -99,6 +102,27 @@ SELECT @ANSI_NULLS AS ANSI_NULLS;
99
102
Requires membership in the **public** role.
100
103
101
104
## Examples
105
+
106
+
The following example uses the Equals (`=`) and Not Equal To (`<>`) comparison operators to make comparisons with `NULL` or `0` and the `null` value in a variable.
107
+
108
+
```sql
109
+
SET ANSI_NULLS OFF
110
+
DECLARE @var INT=NULL
111
+
SELECT
112
+
IIF(@var =NULL, 'True', 'False') as EqualNull,
113
+
IIF(@var <>NULL, 'True', 'False') as DifferentNull,
With `SET ANSI_NULLS ON` all expressions would be evaluated as 'False' because `NULL` cannot be compared with `NULL` or `0` using these operators.
125
+
102
126
The following example uses the Equals (`=`) and Not Equal To (`<>`) comparison operators to make comparisons with `NULL` and non-null values in a table. The example also shows that `IS NULL` is not affected by the `SET ANSI_NULLS` setting.
0 commit comments