I just hate having nulls in my DateTime columns. Having them always mean you also have to handle them in some way in your app.
The most common way is something like this:
public DateTime SomeDate
if (dr["SomeDate"] == DBNull.Value)
if (value == DateTime.MinValue)
dr["SomeDate"] = DBNull.Value;
dr["SomeDate"] = value;
Of course you could write improvements in form of attributes or something similar to reduce the repetetive coding but in the end this had to be implemented.
Yes, having nulls saves space, but disk space is cheap :)
That's why i use the default value for my DateTime columns it's max value which is '99991231 23:59:59:997'. you can use min value if you like it more :)
i use this function for my default values
CREATE FUNCTION dbo.DateTimeMaxValue()
RETURN CONVERT(DATETIME, '99991231 23:59:59:997')
and what would MaxValue be without MinValue
CREATE FUNCTION dbo.DateTimeMinValue()
RETURN CONVERT(DATETIME, '17530101')
i don't know why sql server doesn't already have these functions. If it, by any chance, does have them then i'd love to know about them.
However this again presents some problems since SQL datetime and .Net datetime have different ranges.
If you have SQL Server 2005 and would like to extend your DateTime range to match .Net's read my post here
So we see that you can't simply do
if (.net datetime maxvalue == sql server datetime maxvalue)
In .Net 2.0 we got the System.Data.SqlTypes namespace which gives us native SQL datatypes in .net.
So we also get SqlDateTime, which has a MaxDate and MinDate properties.
SqlDateTime.MaxValue = 9999-12-31 23:59:59.997
SqlDateTime.MinValue = 1753-01-01 00:00:00.000
So you can easily find which dates have the max/min value and handle those anyway you like.
At least something is a bit simplified.
Now i'd just love to be able to do dr["SomeDate"] = SqlDateTime.Null but despite SqlDateTime.Null exists you can't assign it like that.
You still have to use DbNull.Value.