Why, when I insert DateTime null, do I have "0001-01-01" in SQL Server?

I am trying to insert a null (DateTime) value into my database for the field typed by 'date', but I always get '0001-01-01'. I do not understand this field is "allow nulls" and I do not know why I have this default value.

I am using C # asp.net with MVC (Entity Framework), this is my code:

Budget_Synthesis newBS = new Budget_Synthesis { Budget_Code = newBudgetCode, Last_Modified_Date = null }; db.Budget_Synthesis.AddObject(newBS); 

Last_Modified_Date introduced by System.DateTime? therefore, I donโ€™t know why they change this โ€œzeroโ€.

If I try to display the value in my application, I get 01/01/0001 00:00:00

And 0001-01-01 with SSMS

Can someone explain to me why I can't get the real "NULL"?

Best wishes

+7
source share
3 answers

I think this value is null

+9
source

If Last_Modified_Date is of type DateTime , you cannot have "real null" because the DateTime structure - as others have said - is not NULL. Thus, your sample code will not even compile.

Should Last_Modified_Date be of type DateTime? ( Nullable<DateTime> ), your code is correct, but as @Nikola Dimitroff said in your answer, you cannot have a โ€œreal zeroโ€ in your database, since the default is DateTime? - 01/01/0001 00:00:00.

The "real zero" you are looking for is DBNull.Value , but you can only use it for System.DBNull ; if you assign Last_Modified_Date = DBNull.Value , regardless of the type of Last_Modified_Date , your code will not compile.

+4
source

Saying that you are trying to set null to DateTime , are you using Nullable<DateTime> (aka DateTime? ) Or just DateTime ? The last type of value and its default value is exactly 01/01/0001 00:00:00

+2
source

All Articles