12

I try to insert the value null (DateTime) in my database for a field typed 'date' but I always get a '0001-01-01'. I don't understand, this field "allow nulls" and I don't know why I have this default value.

I'm 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 is typed System.DateTime? so I don't know why they change this 'null'.

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

And 0001-01-01 with SSMS

Someone can explain me why I can't get a real 'NULL' ?

Best regards

15
  • 4
    Show your code please May 22, 2013 at 13:09
  • you are inserting a c# value of null datetime into a sqldatetime field? May 22, 2013 at 13:10
  • 3
    How are you trying to insert? This matters. A lot. May 22, 2013 at 13:10
  • 4
    sql server cannot even store 0001-01-01; the minimum value in sql server is January 1, 1753. Where are you seeing this 0001-01-01, exactly? May 22, 2013 at 13:12
  • 2
    Re the latest edit: you show some code that adds an object to what looks to be an ORM of some kind. Right; we're getting somewhere... what ORM is this? What is that AddObject ? How is this all configured? What you haven't told us is anything about how all this gets to the database... May 22, 2013 at 13:16

6 Answers 6

11

I think this is the value corresponding to the null

4
  • 5
    @Andomar which is all well and good, but that isn't what this answer says May 22, 2013 at 13:13
  • @MarcGravell: Comment was meant as additional information. If you run var d = new DateTime(); Console.WriteLine(d);, it prints 0001-01-01, which makes me disagree with the downvotes.
    – Andomar
    May 22, 2013 at 13:17
  • 1
    @Andomar yes, I'm well aware of the zero behaviour of datetime; but without a lot more context from the OP, this is just a random factoid. Indeed, the code now edited into the question shows use of null, which indicates that the type is most likely DateTime?. The default of DateTime? is not 0001-01-01 May 22, 2013 at 13:18
  • 1
    @MarcGravell: Yeah, I presume nHibernate, Linq2Sql or EF converts the null to a DateTime somewhere, and that ends up as 0001-01-01. Perhaps the column is marked as non-nullable in EF. In a way null corresponds to 0001-01-01, which is what this answer says. Might not be a stellar answer, but it's from a new user, and I don't feel this deserves downvotes at all.
    – Andomar
    May 22, 2013 at 13:25
7

If Last_Modified_Date is of type DateTime, you can't have "real null" because DateTime structure - as others already said- is not nullable. So your sample code will not even compile.

If Last_Modified_Date is of type DateTime? (Nullable<DateTime>) your code is correct, but -as @Nikola Dimitroff said in his answer- you can't have "real null" in your database because the default value for DateTime? is 01/01/0001 00:00:00.

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

3

When saying you are trying to put a null DateTime, are you using a Nullable<DateTime> (a.k.a DateTime?) or simply DateTime? The latter is a value type and its default value is precisely 01/01/0001 00:00:00

2
  • Based on the code sample that assigns null to what is presumably a date, we know that it cannot be DateTime; thus DateTime? is the most likely May 22, 2013 at 13:17
  • @NikolaDimitroff It's a System.DateTime? generated by EntityFramework when I linked my Models with the DB, that's why I can give him a 'null' but I don't knwo why he changes it in this default value.
    – Alex
    May 22, 2013 at 13:28
0

As for ADO.NET (and at least as of .NET 4.7), passing null (for example using ExecuteNonQuery) is translated to SQL DEFAULT keyword.

This can be verified using SQL Server Profiler.

For example, the following code:

var sqlCommand = new SqlCommand();
sqlCommand.CommandText = "EXEC CreateLog @UserName, @CreatedOn";
sqlCommand.Parameters.AddWithValue("@UserName", "George");
sqlCommand.Parameters.AddWithValue("@CreatedOn", (DateTime?)null);
sqlCommand.ExecuteNonQuery();

is translated to:

exec sp_executesql N'EXEC CreateLog @UserName, @CreatedOn',
    N'@UserName nvarchar(6), @CreatedOn nvarchar(4000)',
    @UserName=N'George',@CreatedOn=default

This should normally just result in the following error:

The parameterized query [...] expects the parameter '@CreatedOn', which was not supplied.

More details can be found in this answer.

0

When u write the migration , be aware that u type defaultValue:null and make DateTime? in the model class also

public DateTime? FieldEx { get; set; }
migrationBuilder.AddColumn<DateTime>(
               name: "FieldEx",
               table: "tableEx",
               type: "datetime2",
               nullable: true,
               defaultValue:null); 
-3

In python programming language. The return type of the code:

print(type(df['col_name'].get(key='0001-01-01 BC')))
<class 'NoneType'>

It is not null, being more specific it is of NoneType.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.