ERPClimb logoERPClimb

Code examples · CDS views · Beginner

CDS date functions: days requested, a reminder date and an urgency flag

The problem

HR wants every leave request to show how many days it covers, a reminder date 7 days before it starts, and a flag for requests that start within the next 14 days.

How we solve it

  1. 1The viewZI_LeaveDates
  2. 2Read itABAP SQL

The result

A view that returns, for each request, DaysRequested, ReminderDate and StartsSoon.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

HR wants every leave request to show how many days it covers, a reminder date 7 days before it starts, and a flag for requests that start within the next 14 days. The calculation must happen in the database, not in each report.

2. How we design it

Do the date arithmetic once, in a CDS view entity, using the built-in date functions. Every consumer then reads the finished values. The 'today' date comes from the session, so the view needs no parameter.

Why we decided it this way

  1. 1DATS_DAYS_BETWEEN, plus 1The function gives the difference. A request from the 10th to the 12th covers three days, so we add one.
  2. 2DATS_ADD_DAYS with a negative numberSubtracting 7 days is the same as adding minus 7.
  3. 3Fixed error handling 'INITIAL'An invalid date gives an initial date instead of stopping the whole query.
  4. 4$session.system_dateThe reference date is the current date of the application server, like sy-datum. Use $session.user_date if the user's time zone matters.

Before you start

  • The table ZLEAVE_REQ from the managed RAP example, or any table with a start and end date.

3. The code, step by step

Step 1 of 2: Define the view

Three calculated columns next to the plain ones.

CDS · Data definition · ZI_LeaveDates
// Header and plain fields
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Leave requests with date calculations'
define view entity ZI_LeaveDates
  as select from zleave_req
{
  key leave_uuid  as LeaveUuid,
      employee_id as EmployeeId,
      start_date  as StartDate,
      end_date    as EndDate,
      // Days requested
      dats_days_between( start_date, end_date ) + 1 as DaysRequested,
      // Reminder date
      dats_add_days( start_date, -7, 'INITIAL' ) as ReminderDate,
      // Starts soon flag
      case
        when dats_days_between( $session.system_date, start_date ) between 0 and 14
          then 'Y'
        else 'N'
      end as StartsSoon
}
Part by part (plain text)
  1. Header and plain fields. A view entity on the leave table with the key, the employee and the two dates.
  2. Days requested. dats_days_between returns the number of days from the first date to the second. We add 1 because both the first and the last day count.
  3. Reminder date. dats_add_days adds a number of days to a date. A negative number goes back in time. The third argument says what to return if the date is invalid; 'INITIAL' gives an initial date.
  4. Starts soon flag. We compare the days from today to the start date with 14. $session.system_date is the current date of the application server (like sy-datum). A CASE turns the comparison into a flag. Requests that already started count as not 'soon'.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

A view that returns, for each request, DaysRequested, ReminderDate and StartsSoon.

What you should see

Request 2027-03-10 to 2027-03-12: DaysRequested 3, ReminderDate 2027-03-03. A request starting in 5 days: StartsSoon 'Y'. A request starting in 40 days: StartsSoon 'N'.

Try it and check the result

  • Create three leave requests through the RAP example: 10 to 12 March 2027, one starting in 5 days, one starting in 40 days.
  • Run the class. The first row shows DaysRequested 3 and ReminderDate 2027-03-03.
  • The request starting in 5 days shows StartsSoon Y, the one in 40 days shows N.

Common mistakes

  • Forgetting the +1 gives one day too few for a request that includes the last day.
  • $session.system_date is the current date of the application server, so the result changes every day. Do not store it. Use $session.user_date if the user's time zone matters.
  • dats_days_between treats an initial date 00000000 as 0001-01-01 and returns a huge number. Filter such rows or test with dats_is_valid first; only dats_add_days has an error argument.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP CDS view entities, S/4HANA 2020+ and ABAP Environment on BTP. Check on your release.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.