Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 WHERE clause and NULL columns

Author  Topic 

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2008-11-08 : 04:54:23
I want to filter by columns that are not null. This doesn't work for me:

SELECT * FROM [mytable] where price = null

Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-08 : 04:56:20
SELECT * FROM [mytable] where price IS NULL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-08 : 05:01:07
The reason why equality operators like =,<>,>,.. wont work with NULL is because NULL is not internally as a value. NULL just designates lack of value i.e NULL = NULL is also not true. thats why under default settings equality operators wont work with NULL.
However you can override this property by changing ANSI NULL settings. by running below

SET ANSI NULLS OFF

you specify SQL Server to regard NULL as value in which case you can use queries like above to return NULL values.See books online for more details

http://doc.ddart.net/mssql/sql70/set-set_5.htm
Go to Top of Page

shawnmolloy
Yak Posting Veteran

93 Posts

Posted - 2008-11-08 : 05:03:52
Oh yeah, that's right. IS NULL, not = NULL. I always get that mixed up :P

Thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-08 : 05:06:10
Welcome
Go to Top of Page
   

- Advertisement -