Why are NULL values ​​excluded when selecting values ​​from a table

I wrote one sql query to get all records, but not specific ones.

My table is of column3type INTand may also have some null values. But the following queries ignore entries that have NULL in column3.

Table1:

ID | Column1 | Column2 | Column3
1     xyz        xyz       200
2     xyz        xyz       201
3     xyz        xyz       NULL
4     xyz        xyz       NULL
5     xyz        xyz       201

SQL query:

SELECT 
    [ID], 
    [Column1], [Column2], [Column3] // (int, null)
FROM 
    [Table1] 
WHERE
    Column3 != 201

LINQ query:

from tb in _entities.Table1
where tb.Column3 != 201

Since two NULLs cannot be equal ie NULL = NULLalways False. Why do the above queries exclude entries with null values. Only the first record is returned.

+4
source share
4 answers

You need to explicitly indicate that you want NULL values:

from tb in _entities.Table1
where tb.Column3 == null || tb.Column3 != 201

, UNKNOWN TRUE . .

Transact-SQL . ANSI_NULLS ​​ OFF, , NULL = NULL, TRUE. NULL FALSE.

+2

null SQL "". , ", " 201? , .

, , , null. #, null == null.

:

where tb.Column3 == null || tb.Column3 != 201
+1

NULL ( NULL) , false. , NULL!= 201 . - :

where tb.Column3 is null or tb.Column3 != 201

isnull:

where isnull(tb.Column3, 0) != 201
0
source

SQL Server NULL-, #. #, - != 201, . SQL Server, - != 201, . SQL Server, NULL != 201

from tb in _entities.Table1
where tb.Column3 == null || tb.Column3 != 201
0
source

Source: https://habr.com/ru/post/1649247/


All Articles