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!

Function to Calculate Business Minutes Between Two Dates

sliderrulesJul 4 2013 — edited Jul 12 2013

Hi,

I am using Oracle Database version 11.2.1. I would like to calculate the number of working minutes between two dates where the working hours are Monday - Thursday 8.45am to 17:00pm & Fridays 8:45am to 16:30pm

I found the following function useful in a similar post but wanted some help incorporating the above working days and hours into account

CREATE OR REPLACE
  FUNCTION get_bus_minutes_between(
                                   p_start_date DATE,
                                   p_end_date DATE
                                  )
    RETURN NUMBER
    IS
        v_return NUMBER;
    BEGIN
        with t as (
                   select  case level
                             when 1 then greatest(p_start_date,trunc(p_start_date) + 9 / 24)
                             else trunc(p_start_date) + level - 15 / 24
                           end start_dt,
                           case connect_by_isleaf
                             when 1 then least(p_end_date,trunc(p_end_date) + 18 / 24)
                             else trunc(p_start_date) + level - 8 / 24
                           end end_dt
                     from  dual
                     connect by level <= trunc(p_end_date) - trunc(p_start_date) + 1
                  )
        select  sum(greatest(end_dt - start_dt,0)) * 24 * 60 work_minutes
          into  v_return
          from  t
          where trunc(start_dt) - trunc(start_dt,'iw') < 5; -- exclude weekends
        RETURN v_return;
END;
/

Can anyone help?

This post has been answered by Frank Kulash on Jul 11 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2013
Added on Jul 4 2013
21 comments
3,366 views