Working with non-nullable types in C# can be a bit of a pain. For instance when I have a date as a string and need to parse it into a DateTime what should the value be if the parse fails? I can’t use null because DateTime is not a nullable type.
This is exactly the dilema I encountered today. No worries, I’ll use DateTime.MinValue that way it is constant and I don’t have to worry about being consistent if I had chosen an arbitrary value of my own.
Well as it turns out I did have some worries. Sql Server 2000’s minimum DateTime value is not the same, in fact it is quite different. This kept causing errors.
For your reference here are some values you should take note of.
DateTime myDate = DateTime.MinValue; //=> 1/1/0001
SqlDateTime mySqlDate = SqlDateTime.MinValue; //=> 1/1/1753
//also note that SQL Server's smalldatetime min value is 1/1/1900
So my problem was easily averted, after a quick Google search. I just had to use SqlDateTime.MinValue instead of DateTime.MinValue.
Cheers
Popularity: 29% [?]

March 26th, 2008 at 5:51 pm
Why don’t you use a nullable date time?
DateTime? whateverDate = null;
The question mark makes the datetime nullable, it actually works for any struct type even Enums.
March 26th, 2008 at 6:17 pm
Had I known that little gem I probably would have :D. I just switched from .Net 1.1 about 6 months ago so I don’t know all the newest features yet.
March 30th, 2008 at 5:26 pm
yeah this has been a savior many times for me.
March 30th, 2008 at 7:36 pm
Hey,
I posted about this subject few month ago.
Take a look at the link:
http://mironabramson.com/blog/post/2007/09/Caution-When-passing-Null-or-DateTime-into-Store-Procedure.aspx
March 30th, 2008 at 11:43 pm
Still good to know though… I’ve been in situations where I haven’t wanted to make a db datetime nullable, so this info is very handy. Cheers Justin.
March 31st, 2008 at 1:11 am
If you’re not sure if a string will be parsed as a DateTime you should be using DateTime.TryParse().
March 31st, 2008 at 2:01 am
@Adrian, this is true but if the parse fails it assigs a value of DateTime.MinValue which causes an SQL error which is the situation that lead to this post int he first place. Sadly SqlDateTime does not have a TryParse method.
April 1st, 2008 at 1:35 am
“Sadly SqlDateTime does not have a TryParse method.”
Sounds like a good example for an Extension method…
April 1st, 2008 at 4:23 am
@Ian, indeed it does. I’ll see what I can whip up in the next couple days. Feel free to post any advice.
April 2nd, 2008 at 10:54 am
[…] talked about an issue I ran into between DateTime.MinValue and SqlDateTime.MinValue in a previous post. One other point I noticed while researching the topic was that, unlike almost […]
April 6th, 2008 at 2:30 pm
[…] Daily gives us the warning C# DateTime.MinValue is not the same as SqlDateTime.MinValue. Good to […]
April 18th, 2008 at 7:32 am
[…] Deshabilitar el chequeo de compatibilidad de extensiones en las betas de firefox 3 Inside c# (valores por defecto) Patrones (Patrón provider) Métodos de extensión (String formatting with named variables) Si […]