These two queries return different order counts and it’s not entirely clear as to why. The first where clause is the accepted correct version:
where
year(OrderDate) = 2011
and MONTH(OrderDate) = 8
and DAY(OrderDate) = 3
but if I say something very similar:
WHERE
io.OrderDate >= '2011-08-03 00:00:00'
and io.OrderDate <= '2011-08-03 11:59:59'
I get a completely different record count. Is there something I’m doing wrong in date formatting or is there something I’m missing in logic? I’d prefer to say the latter as it’s a little easier to use IMHO.
EDIT:
The new answer I would recommend combines my original (as displayed below) with the comments made by Mikael and Alex.
Should provide the desired result.
The second query is only looking from Midnight to Noon. (00:00:00 – 11:59:59 is twelve hours)
Should fix it.
@Alex answer has a point as well. Milliseconds count (depending on whether or not your particular table includes them).
Would include any additional records missed