Mathematically the solution could be
Div(DaysBetween(,),7) * 5 + f[Mod(DaysBetween(,),7)]
where f is a function that deals with starting and/or ending on weekends. (Here’s me pretending I know maths
)
Gotta have a few rules before setting out on this quest. Let’s say no timestamps - midnights only - so if the dates are the same day no days have passed. And date2 > date1 for simplicity. Given this we can get something to work as a variable in reports:
=(Floor(()/7)*5)+(If (If
DayNumberOfWeek()=DayNumberOfWeek() Then 0 Else If DayNumberOfWeek()=6 Then (If DayNumberOfWeek()=7 Then 0 Else Mod( ,7)-2) Else If DayNumberOfWeek()=7 Then Mod( ,7)-1 Else Mod( ,7))=6 Then 5 Else (If DayNumberOfWeek()=DayNumberOfWeek() Then 0 Else If DayNumberOfWeek()=6 Then (If DayNumberOfWeek()=7 Then 0 Else Mod( ,7)-2) Else If DayNumberOfWeek()=7 Then Mod( ,7)-1 Else Mod( ,7)))
Where the variable is DaysBetween(,).
This actually translates into a simpler looking piece of Oracle SQL which I suppose could be used in an object in very simple, non performance intensive reports
(FLOOR((date2-date1)/7)*5)+DECODE(DECODE(to_char(date1,‘D’),to_char(date2,'D '),0,7,DECODE(to_char(date2,‘D’),1,0,MOD(date2-date1,7)-2),1,MOD(date2-date1 ,7)-1,MOD(date2-date1,7)),6,5,DECODE(to_char(date1,‘D’),to_char(date2,‘D’),0 ,7,DECODE(to_char(date2,‘D’),1,0,MOD(date2-date1,7)-2),1,MOD(date2-date1,7)- 1,MOD(date2-date1,7)))
And at the end of all this work, it would still be easier still to have a lookup table with flags for weekdays, which of course could take care of holidays. And I have no doubt there are briefer ways of dealing with the logic, so I’m looking for them.
Thanks for the puzzle,
Paul
From: DRathbun@AOL.COM [SMTP:DRathbun@AOL.COM] Sent: Friday, March 02, 2001 4:02 AM
In a message dated Thu, 1 Mar 2001 7:01:49 AM Eastern Standard Time, Ted Michalski tmichal1@FORD.COM writes:
<< Here’s a good one. I need to calculate the number of days between two dates. Normally not a problem… but I don’t want to count weekends in the calculation. So if I wanted to get the number of days between today (Wednesday) and last Tuesday, I need to result to be 6 (8 total days - the two weekend days). Has anyone ever had to do this?
Here’s something to get you started: it’s not perfect, but you should be able to take the idea and run with it.
=(Week() - Week()) * 5) + Mod(DaysBetween(, ), 7)
The Week() function returns the week number of a date. So in your example of Wednesday this week and Tuesday last week, the week could be 10 and 9; 10-9 is 1, times 5 is five days for the included “week’s” time.
The DaysBetween(, ) returns 8. Mod (8, 7) returns 1, or one additional day. So five days (for the one week) plus 1 additional day = 6 days.
Looks good so far… why did I say it was a starting point? Because you need to handle the special situation where date 2 is in a different year from date 1. If date 2 is Jan 4, and date 1 is Dec 30, then the week numbers may be 1 and 52 (or 53). You need to adjust accordingly.
Listserv Archives (BOB member since 2002-06-25)