Ozi Oz Ozi Oz - 3 months ago 9
SQL Question

Insert null/empty value in sql datetime column by default

I have a question please:
How can 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...

Thanks

Answer

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', ..);