tags:

views:

7142

answers:

3

This might be simple, but I cant figure out how to do a simple DateTime with a NOT NULL?

I want to do something like this:

SELECT * FROM someTable WHERE thisDateTime IS NOT NULL

But that obviously doesn't work. I am using MS SQL 2005 if that matters. Thank you.

+1  A: 

erm it does work? I've just tested it?

/****** Object:  Table [dbo].[DateTest]    Script Date: 09/26/2008 10:44:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[DateTest](
    [Date1] [datetime] NULL,
    [Date2] [datetime] NOT NULL
) ON [PRIMARY]

GO
Insert into DateTest (Date1,Date2) VALUES (NULL,'1-Jan-2008')
Insert into DateTest (Date1,Date2) VALUES ('1-Jan-2008','1-Jan-2008')
Go
SELECT * FROM DateTest WHERE Date1 is not NULL
GO
SELECT * FROM DateTest WHERE Date2 is not NULL
John Nolan
I concur, I tested it as well.
Abbas
A: 

As had already been indicated, what your are trying should work. Perhaps you can post the specifics of the problem... what error msg for example.

CJM
A: 

Just to rule out a possibility - it doesn't appear to have anything to do with the ANSI_NULLS option, because that controls comparing to NULL with the = and <> operators. IS [NOT] NULL works whether ANSI_NULLS is ON or OFF.

I've also tried this against SQL Server 2005 with isql, because ANSI_NULLS defaults to OFF when using DB-Library.

Mike Dimmick