The documentation states that the database ANSI_NULLS flag controls the behavior of comparisons with null on some side.
I was checking this stack overflow post to check how to determine (not set) the value of this flag. Interestingly, not all of the answers seemed to work for me.
My test query:
create table x(id int,txt nvarchar(max))
insert x(id,txt) values (1,'not null here'),(2,null)
select * from x
--query 1
select * from x where txt=null
--query 2
select * from x where txt<>null
The output returned is two empty result sets. From this, I can logically deduce ANSI_NULLS is ON in my database.
Now, to the checking options:
1
select databasepropertyex('MyDatabaseName', 'IsAnsiNullsEnabled')
Returns 0. I did not expect this.
2
DECLARE @options INT
SELECT @options = @@OPTIONS
IF ( (32 & @options) = 32 ) PRINT 'ANSI_NULLS'
Prints "ANSI_NULLS". Expected.
3
SELECT is_ansi_nulls_on FROM sys.databases WHERE name = 'MyDatabaseName'
Returns 0. I did not expect this.
4
dbcc useroptions
The results set includes a row with [Set Option]='ansi_nulls' and [Value]='Set'. Expected.
Why do do options 1 and 3 give me this result?
My @@version is:
Microsoft SQL Server 2017 (RTM-GDR) (KB4505224) - 14.0.2027.2 (X64)
Jun 15 2019 00:26:19
Copyright (C) 2017 Microsoft Corporation
Developer Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor)