The problem is rounding the SQL Server datetime
type as described by AntP.
There are two different solutions that you might consider:
Option number 1
Use the datetime2
type in SQL Server, as Tim suggested. It has higher accuracy, so you can hardly round it. This is still tricky since you need to know how much accuracy you are sending and how much the type will support. In other words, should it be 23:59:59.999
or should it be 23:59:59.999999
or will it be 23:59:59.0
enough? You will need to decide what makes sense for your application.
If your data always contains integer dates, you can change your input value to:
DateTime endDate = startdate.AddDays(1).AddSeconds(-1);
And it will not be rounded, even with datetime
type.
Option number 2
Use half-open intervals [start,end)
. When the end date is exclusive, your inquiries are much simpler, and you do not need to worry about accuracy. When two intervals border each other, the end of one interval will be exactly the same as the beginning of the next. There is no ambiguity because the end date is exclusive.
Instead of sending the range from 09/03/2013 00:00:00
to 09/03/2013 23:59:59
you send it as 09/03/2013 00:00:00
at 09/04/2013 00:00:00
with the understanding that the exact end date is excluded.
In other words, the date is in the range if:
StartOfRange <= @TheDate < EndOfRange
Or add other terms:
StartOfRange <= @TheDate AND EndOfRange > @TheDate
On the .NET side, you can still imagine your baseline as fully inclusive. Just add the appropriate value to the end before passing it to SQL. For example, if you request integer dates as input, then add one day to the end.