Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLServer Get Results Where Value Is Null

I have an SQL server database that I am querying and I only want to get the information when a specific row is null. I used a where statement such as:

WHERE database.foobar = NULL

and it does not return anything. However, I know that there is at least one result because I created an instance in the database where 'foobar' is equal to null. If I take out the where statement it shows data so I know it is not the rest of the query.

Can anyone help me out?

like image 554
Josh Mein Avatar asked Jul 16 '26 22:07

Josh Mein


2 Answers

Correct syntax is WHERE database.foobar IS NULL. See http://msdn.microsoft.com/en-us/library/ms188795.aspx for more info

like image 98
bdukes Avatar answered Jul 18 '26 11:07

bdukes


Comparison to NULL will be false every time. You want to use IS NULL instead.

x =  NULL      -- always false
x <> NULL      -- always false

x IS NULL      -- these do what you want
x IS NOT NULL
like image 24
Mark Harrison Avatar answered Jul 18 '26 13:07

Mark Harrison