Recently I had a strange bug in a project for a report page for closed cases. For whatever reason a case wasn't being included.
Long story short, I was querying for the cases using dates for the start and end of the month, but the last day of the month was being excluded. For example a case with a closedDate of 9/30/12 2:15:12am would not be returned with the following query:
Date startOfMonth = Date.newInstance(2012,9,1);
Date endOfMonth = Date.newInstance(2012,9,30);
List<Case> cases = [
select id from Case
where closedDate >= :startOfMonth
and closedDate <= :endOfMonth];
The issue ended up being that this type of query converted the dates into date times, specifically 9/1/12 12:00:00am and 9/30/12 12:00:00am, which looks good at first, but what we really want to get all cases on the last day is 9/1/12 12:00:00am and 9/30/12 11:59:59pm. I was able to work around by doing this.
DateTime startOfMonthDT = DateTime.newInstance(2012,9,1,0,0,0);
DateTime endOfMonthDT = DateTime.newInstance(2012,9,30,23,59,59);
List<Case> cases = [
select id from Case
where closedDate >= :startOfMonthDT
and closedDate <= :endOfMonthDT];
That said, regardless of whether this is expected query behavior, it feels like there has to be a safer way to do this date range queries that isn't such a gotcha for developers. Are there any better ways to write the original query without doing the all the extra math to get the exact date time for the first and last second of the month?
endOfMonthDT
tobeginningOfNextMonth
, so it is clearer that the query gets everything from the start of this month, inclusive, to the beginning of next month, exclusive. Note that less than, not less than or equal to, is used.