Skip to content

Commit 1aebf1e

Browse files
Merge branch 'patch-52' of https://github.com/jovanpop-msft/sql-docs-pr into jovanpop-msft-patch-52
2 parents ca95f41 + b9bd2b8 commit 1aebf1e

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

docs/t-sql/statements/set-ansi-nulls-transact-sql.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: "SET ANSI_NULLS (Transact-SQL)"
33
description: SET ANSI_NULLS (Transact-SQL)
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
6-
ms.date: 04/04/2024
6+
ms.date: 04/28/2025
7+
ms.reviewer: jovanpop, randolphwest
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -27,6 +28,8 @@ monikerRange: ">=aps-pdw-2016||=azure-sqldw-latest||>=sql-server-2016||>=sql-ser
2728
[!INCLUDE [sql-asdb-asdbmi-asa-pdw](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw.md)]
2829

2930
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.
3033

3134
> [!NOTE]
3235
> `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;
99102
Requires membership in the **public** role.
100103

101104
## 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,
114+
IIF(@var = 0, 'True', 'False') as EqualZero,
115+
IIF(@var <> 0, 'True', 'False') as DifferentZero
116+
```
117+
118+
The results are show in the following table.
119+
120+
| EqualNull | DifferentNull | EqualZero | DifferentZero |
121+
| --- | --- | --- | --- |
122+
| True | False | False | True |
123+
124+
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+
102126
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.
103127

104128
```sql

0 commit comments

Comments
 (0)