ABAP - Keyword Documentation →  ABAP - Dictionary →  ABAP CDS in ABAP Dictionary →  ABAP CDS - Data Definitions →  ABAP CDS - DDL Statements →  ABAP CDS - DEFINE VIEW →  ABAP CDS - SELECT →  ABAP CDS - SELECT, Predefined Functions →  ABAP CDS - Special Functions →  ABAP CDS - Date Functions and Time Functions → 

ABAP CDS - Date Functions

Syntax

... DATS_IS_VALID(date)
  | DATS_DAYS_BETWEEN(date1,date2)
  | DATS_ADD_DAYS(date,days,on_error)
  | DATS_ADD_MONTHS(date,months,on_error) ...


Variants:

1. ... DATS_IS_VALID(date)

2. ... DATS_DAYS_BETWEEN(date1,date2)

3. ... DATS_ADD_DAYS(date,days,on_error)

4. ... DATS_ADD_MONTHS(date,months,on_error)

Effect

These functions perform operations with arguments of the predefined data type DATS. The functions have positional parameters to which actual parameters need to be assigned when called. There are currently no optional parameters. Suitable fields of a data source, literals, parameters, path expressions, predefined functions, or expressions can all be specified as actual parameters. Only literals can be passed to the parameter on_error. If an actual parameter contains the null value, every function except DATS_IS_VALID returns a null value.

Note

It is not currently possible to access the current system date directly in a CDS view. Instead, a CDS view can be given an appropriate input parameter. The special annotation @Environment.systemField makes it possible to pass the value of the ABAP system field sy-datum to this parameter.

Variant 1

... DATS_IS_VALID(date)


Effect

The function DATS_IS_VALID determines whether date (if specified) contains a valid date in the format YYYYMMDD. The actual parameter must have the predefined data type DATS. The result has the data type INT4. A valid date produces the value 1 and all other input values (including the null value) produce the value 0.

Note

The value "00010101" is a valid date but the value "00000000" is not.

Variant 2

... DATS_DAYS_BETWEEN(date1,date2)


Effect

The function DATS_DAYS_BETWEEN calculates the difference between two specified dates, date1 and date2, in days. The actual parameters must have the predefined data type DATS and should contain a valid date in the format YYYYMMDD. Any invalid dates specified are initialized or set to the value "00010101" before the calculation. The result has the data type INT4. If date2 is greater than date1, the result is positive. In the reverse case, it is negative.

Note

Before the difference is calculated, the specified dates are converted to integers, like in ABAP, and the corresponding rules apply.

Variant 3

... DATS_ADD_DAYS(date,days,on_error)


Effect

The function DATS_ADD_DAYS adds days days to a specified date date.

The format is not case-sensitive. Any incorrectly specified values raise an exception.

The result has the data type DATS. If days is positive, the number of days is added to date. In other cases, the number of days is subtracted. If the calculation produces an invalid date, the error is handled as specified in on_error.

Note

For the calculation, the specified date is converted to an integer, like in ABAP, and the result is converted to a date again while applying the corresponding rules.

Variant 4

... DATS_ADD_MONTHS(date,months,on_error)


Effect

The function DATS_ADD_MONTHS adds months months to a specified date date.

The result has the data type DATS. If months is positive, the number of months is added to date. In other cases, the number of months is subtracted.

An attempt is made to create a date with the same day in an earlier or later month. If the maximum day possible in a month is exceeded, the greatest possible day is used. If the calculation produces an otherwise invalid date, the error is handled as specified in on_error.

Example

The following CDS view applies date functions in the SELECT list to columns of the database table DEMO_EXPRESSIONS. The program DEMO_CDS_DATE_FUNCTIONS uses SELECT to access the view. The columns DATS1 and DATS2 in the database table and the actual parameters for the input parameters of the view can be given any values. In the case of DATS_ADD_DAYS and DATS_ADD_MONTHS. invalid values or values that produce invalid results are handled as specified in the view.

@AbapCatalog.sqlViewName: 'demo_cds_datfnc'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_date_functions
  with parameters p_days:abap.int4,
                  p_months:abap.int4
  as select from demo_expressions {
    id,
    dats1 as date1,
    dats_is_valid(dats1) as valid1,
    dats2 as date2,
    dats_is_valid(dats2) as valid2,
    dats_days_between(dats1,dats2) as difference,
    dats_add_days(dats1,:p_days,'INITIAL')  as day1,
    dats_add_months(dats2,:p_months,'FAIL') as day2 }