Skip to Main Content
  • Questions
  • Subtract date between a given start date and end date

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, ashish.

Asked: December 07, 2019 - 1:37 am UTC

Last updated: December 09, 2019 - 11:33 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi Tom,
we have leave management system where user can select a date range to schedule his leaves.
Ex. User a inputs date as 05-May-2019 to 23-Jul-2019. Later on he can cancel few days from this date like, 30-May-2019 to 31-May-2019.

How can I map his leave as 05-May-2019 to 29-May-2019 and then 01-Jun-2019 to 23-Jul-2019.

User has the ability to cancel a single day or multiple days by giving a date range.

Thank you for your help

and Chris said...

Here's one way to solve it:

Store every day a user is scheduled to work. With a column to state if this is holiday or not, e.g.:

create table employee_work (
  employee_id integer
    not null,
  working_day date
    not null
    check ( working_day = trunc ( working_day ) ),
  is_holiday  integer
    default 0
    not null
    check ( is_holiday in ( 0, 1 ) ),
  primary key (
    employee_id, working_day
  )
);


Load it up with every employee's working days as needed.

With this in place, when they request holiday run:

update employees
set    is_holiday = 1
where  employee_id = :emp
and    working_day between to_date ( :start_dt, <fmt> ) 
                   and to_date ( :end_dt, <fmt> );


And if they want to cancel days within a holiday period, set those days back to "not holiday" in the range:

update employees
set    is_holiday = 0
where  employee_id = :emp
and    working_day between to_date ( :start_dt, <fmt> ) 
                   and to_date ( :end_dt, <fmt> );


This avoids the need to do any manipulation of the existing date ranges.

If you want to show someone which days they have booked holiday, run:

select * from employees
where  employee_id = :emp
and    is_holiday = 1


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.