Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert null/empty value in sql datetime column by default

Tags:

sql

How do I create a table in SQL server with the default DateTime as empty, not 1900-01-01 00:00:00.000 that I get?

I mean, if there is no value inserted, the default value should be null, empty, etc.

like image 522
Ozi Oz Avatar asked Dec 18 '12 14:12

Ozi Oz


2 Answers

if there is no value inserted, the default value should be null,empty

In the table definition, make this datetime column allows null, be not defining NOT NULL:

...
DateTimeColumn DateTime,
...

I HAVE ALLOWED NULL VARIABLES THOUGH.

Then , just insert NULL in this column:

INSERT INTO Table(name, datetimeColumn, ...)
VALUES('foo bar', NULL, ..);

Or, you can make use of the DEFAULT constaints:

...
DateTimeColumn DateTime DEFAULT NULL,
...

Then you can ignore it completely in the INSERT statement and it will be inserted withe the NULL value:

INSERT INTO Table(name, ...)
VALUES('foo bar', ..);
like image 148
Mahmoud Gamal Avatar answered Oct 15 '22 23:10

Mahmoud Gamal


Even if your column is nullable, inserting an empty string will set the value to 1900-01-01 00:00:00.000. Make you insert real nulls not strings, then the db will store a null.

like image 41
Matt Avatar answered Oct 16 '22 00:10

Matt