Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server : why doesn't this query return NULL values?

I have a table with one column named value, of type int, and 4 rows in it:

20
50
NULL
0

Why does this query return 20 and 50 only? I need NULL values, too.

SELECT * 
FROM dbo.myTable
WHERE value != 0
like image 295
quelquecosa Avatar asked Dec 03 '22 21:12

quelquecosa


2 Answers

In SQL, NULL is a special value, nothing can ever be equal to or not equal to it. In fact, NULL doesn't even equal itself. When searching for null values, you must use the IS operator. So you need to change your query to include them:

SELECT * 
FROM dbo.myTable
WHERE value !=0
OR value IS NULL
like image 154
DavidG Avatar answered Jan 05 '23 07:01

DavidG


You can also replace null for sth else what is not a zero using isnull or coalesce

SELECT * 
FROM dbo.myTable
WHERE ISNULL(value,1) <> 0 
like image 32
pi.314 Avatar answered Jan 05 '23 07:01

pi.314