Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a SQL NOT NULL with a DateTime?

Tags:

sql-server

How does one handle a DateTime with a NOT NULL?

I want to do something like this:

SELECT * FROM someTable WHERE thisDateTime IS NOT NULL

But how?

like image 944
naspinski Avatar asked Sep 26 '08 09:09

naspinski


People also ask

Can date not be NULL SQL?

Description. The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

How do you check if a DateTime field is not null or empty in SQL?

Use model. myDate. HasValue. It will return true if date is not null otherwise false.

Can a DateTime field be NULL?

DateTime is a Value Type like int, double etc. so there is no way to assigned a null value.

How do I pass a date as NULL in SQL?

In SQL Server just insert NULL in the Datetime column: INSERT INTO Table(name, datetimeColumn, ...) VALUES('foo bar', NULL, ..);


2 Answers

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
like image 161
Johnno Nolan Avatar answered Sep 28 '22 15:09

Johnno Nolan


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.

like image 21
Mike Dimmick Avatar answered Sep 28 '22 15:09

Mike Dimmick