Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Count days between two dates, excluding Saturday and Sunday

1518230Jan 18 2014 — edited Jan 19 2014

Was reading through archived posts and found this one:

rp0428Guru

>

Hi all he lp me in getting a oracle query to find out the days between 2 input dates excluding the weekends...

>

Something like this should work

with dates as (select sysdate fromDate, sysdate + 10 toDate from dual),   days as (select sysdate + level - 0 myDay from dates            connect by level < toDate - fromDate) select count(*) from days where to_char(myDay, 'Dy') not in ('Sat', 'Sun') COUNT(*) 6

The 'dates' query just provides the start and end dates. The 'days' query produces a date value for each day in the range between the start and end.

Then the last query counts those days if they are not a Saturday or Sunday.

You can adjust the 'days' query depending on whether you want both the start and end date included. For example if start is today and end is tomorrow (Friday) the above query result will be '1'.

Are you forgetting to mark your questions ANSWERED when they have been? Total Questions: 12 (10 unresolved)


I pasted the code above into Pentaho 3.8 and query worked fine, returning 6. When I tried entering sample dates however, it returned 0.

with dates as (select 01/10/2014 fromDate, 01/18/2014 toDate from dual),

  days as (select sysdate + level - 0 myDay from dates

           connect by level < toDate - fromDate)

select count(*) from days where to_char(myDay, 'Dy') not in ('Sat', 'Sun')

Can anyone spot what I am doing wrong? I am trying to enter sample dates so I can verify functionality. This will allow me to move closer to using the fields I actually need for report. fromDate will eventually be CREATED_DATE and toDate will be DATE_FIRST_SIGNED.



Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 16 2014
Added on Jan 18 2014
3 comments
1,742 views