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
 General SQL Server Forums
 New to SQL Server Programming
 How to Compare DATETIME data with null values

Author  Topic 

freephoneid
Yak Posting Veteran

52 Posts

Posted - 2010-03-17 : 19:20:19
Hi,
I observed a weird thing...I don't have much exposure to SQL server. When I write the below query, it returns proper records with null values

col is of type DATETIME

SELECT * FROM table WHERE col = null

But when I run below query, it does not return any records. Why is that so?

SELECT * FROM table WHERE col = NULL AND DATEADD(DAY, 10, col) >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

I'm trying to get results for all records whose col is null and col+10days >= Current date.

Please note that vendors & products are views.

Appreciate your help!

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-17 : 19:21:37
You should be using: WHERE col IS NULL

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-03-18 : 06:17:23
And a column value cannot be BOTH null and have a value at the same time,
so I suggest you change the AND to OR instead.



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -