Calculate Working days in a month

I wrote a report about half a year ago in which one of the requirements was to give the number of weekdays between two dates. The way I did it was as below (and yes, the code was quite messy):

  1. Get the two days - date 1 and date 2
  2. Get total days between the two days (date 2 minus date 1)
  3. Get the floor value of the total number of days between the two dates divided by seven, and times this by five (this counts all whole weeks in the range as five working days)
  4. Get the remainder (using the MOD function) of the total number of days divided by seven
  5. The fiddly bit: set up a big CASE statement to add on additional days based on the remainder and the day of the week of the first date (using TO_CHAR(date1, ‘DY’) ). For example, if the first date is a Monday and there is a remainder of 3, add 3 days (for Monday, Tuesday and Wednesday). But if your first date is a Thursday and your remainder is 5, you would only add 3, as you only want to count the Thursday, Friday and Monday and ignore the Saturday and Sunday.

Also, as if that wasn’t fiddly enough, the above only works if date 2 is greater than date 1, but once you’ve got the above done it should be straightforward to work out the conditions to check which date’s highest.

Any questions on the above, please feel free to ask - hope this helps.


Olly :uk: (BOB member since 2008-01-24)