SQL Server Date Comparison

In SQL Server, the dates are stored as datetime. For instance, 12/31/2011 will be stored as 12/31/2011 00:00:00. If the time part is filled out, which by default it is, then you’ll see a time like 12/31/2011 13:01:33.

This throws a wrench in comparing dates without the time. Many people do a conversion to a varchar to both sides of the compare like so: convert(varchar(10), MyDateTimeField, 101)DateDiff(day, MyFirstDateTimeField, MySecondDateTimeField) < 0

In my opinion, the code is cleaner, and I believe/hope that this internal method is optimized for this comparison.


Leave a Reply