Insert null/empty value in sql datetime column by default

后端 未结 5 589
死守一世寂寞
死守一世寂寞 2020-12-01 23:52

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,

5条回答
  •  猫巷女王i
    2020-12-02 00:24

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

提交回复
热议问题