SAP CDS DATE FUNCTIONS V2



Get Example source ABAP code based on a different SAP table
  


• DATN_DAYS_BETWEEN ABAP_CDS_FUNCTION
• DATN_ADD_DAYS ABAP_CDS_FUNCTION
• DATN_ADD_MONTHS ABAP_CDS_FUNCTION
• DATS_IS_VALID ABAP_CDS_FUNCTION
• DATS_DAYS_BETWEEN ABAP_CDS_FUNCTION
• DATS_ADD_DAYS ABAP_CDS_FUNCTION
• DATS_ADD_MONTHS ABAP_CDS_FUNCTION

ABAP_CDS_DDL - CDS View Entity, Date Functions

ABAP_SYNTAX
... DATN_DAYS_BETWEEN(date1,date2)
$| DATN_ADD_DAYS(date,days)
$| DATN_ADD_MONTHS(date,months)
$| DATS_IS_VALID(date)
$| DATS_DAYS_BETWEEN(date1,date2)
$| DATS_ADD_DAYS(date,days,on_error)
$| DATS_ADD_MONTHS(date,months,on_error) ...

ABAP_VARIANTS:
1 ... DATN_DAYS_BETWEEN(date1,date2)
2 ... DATN_ADD_DAYS(date,days)
3 ... DATN_ADD_MONTHS(date,months)
4 ... DATS_IS_VALID(date)
5 ... DATS_DAYS_BETWEEN(date1,date2)
6 ... DATS_ADD_DAYS(date,days,on_error)
7 ... DATS_ADD_MONTHS(date,months,on_error)

What does it do?
In a CDS view entity, these functions execute operations with arguments of the built-in data types DATN and DATS. The functions have positional parameters to which actual parameters must be assigned when called. There are currently no optional parameters. Suitable fields of a data source, literals, session variables, parameters, path expressions, built-in functions, SQL-based scalar functions, reuse expressions using $projection, 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.



Latest notes:

The session variable $session.system_date is used in a CDS view entity to provide direct access to the current system date. The session variable $session.user_date returns the current user date. In addition, the special annotation @Environment.systemField makes it possible to pass the value of the ABAP system field sy-datum to input parameters.
The session variables $session.user_date and $session.system_date return the data type DATS. They can be specified as actual parameters in all functions that expect data type DATS as input. They can be converted to data type DATN with the conversion function DATS_TO_DATN.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_1 ... DATN_DAYS_BETWEEN(date1,date2)

What does it do?
The function DATN_DAYS_BETWEEN calculates the difference between two specified dates, date1 and date2, in days. The actual parameters must have the built-in data type DATN and must contain a valid date in the format YYYYMMDD . They can be specified as literals, as fields of a data source, parameters, or as path expressions. To use a session variable, it must be converted to data type DATN using the conversion function DATS_TO_DATN. The result has the data type INT4 . If date2 is greater than date1, the result is positive. In the reverse case, the result is negative. If the result is outside of the range of valid dates, an error occurs.



Latest notes:

Empty literals are not possible.
If an actual parameter is assigned the literal input '00000000', it is set to the initial value and the value '00010101' is used for the calculation.
The days missing from the Gregorian calendar (from 5.10.1582 to 15.10.1582) are handled as usual in ABAP.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_2 ... DATN_ADD_DAYS(date,days)

What does it do?
The function DATN_ADD_DAYS adds days days to a specified date date.
The actual parameter date must have the built-in data type DATN and is must contain a valid date in the format YYYYMMDD. It can be specified as typed literal, field of a data source, parameter, or as path expression. To use a session variable, it must be converted to data type DATN using the conversion function DATS_TO_DATN.
The actual parameter days must have the built-in data type INT4.
The result has the data type DATN . If days is positive, the number of days is added to date . Otherwise, the number of days is subtracted. If the calculation produces an invalid date, an error occurs.



Latest notes:

The days missing from the Gregorian calendar (from 5.10.1582 to 15.10.1582) are handled as usual in ABAP.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_3 ... DATN_ADD_MONTHS(date,months)

What does it do?
The function DATN_ADD_MONTHS adds months months to a specified date date.
The actual parameter date must have the built-in data type DATN and it must contain a valid date in the format YYYYMMDD. It can be specified as typed literal, field of a data source, parameter, or as path expression. To use a session variable, it must be converted to data type DATN using the conversion function DATS_TO_DATN.
The actual parameter months must have the built-in data type INT4.
The result has the data type DATN . If months is positive, the number of months is added to date. Otherwise, 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 target month has fewer days than the source month, the last day of the target month is returned. If the calculation produces an invalid date, an error occurs.



Latest notes:

The days missing from the Gregorian calendar (from 5.10.1582 to 15.10.1582) are handled as usual in ABAP.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_4 ... DATS_IS_VALID(date)

What does it do?
The function DATS_IS_VALID determines whether date contains a valid date in the format YYYYMMDD. The actual parameter must have the built-in data type DATS. It can be specified as literal , as field of a data source, parameter, or as path expression. 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.



Latest notes:

The value '00010101' is a valid date but the value '00000000' is not.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_5 ... DATS_DAYS_BETWEEN(date1,date2)

What does it do?
The function DATS_DAYS_BETWEEN calculates the difference between two specified dates, date1 and date2, in days. The actual parameters must have the built-in data type DATS and should contain a valid date in the format YYYYMMDD. They can be specified as literals, as fields of a data source, parameters, session variables, or as path expressions. The result has the data type INT4. If date2 is greater than date1, the result is positive. In the reverse case, it is negative.



Latest notes:

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

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

What does it do?
The function DATS_ADD_DAYS adds days days to a specified date date.
The actual parameter date must have the built-in data type DATS and should contain a valid date in the format YYYYMMDD.
The actual parameter days must have the built-in data type INT4.
The actual parameter on_error must have the built-in data type CHAR with the length 10 and must have one of the following values:
'FAIL' (an error raises an exception)
'NULL' (an error returns the null value)
'INITIAL' (an error returns the initial value)
'UNCHANGED' (an error returns the unmodified value of date )
The values are 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.



Latest notes:

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.
NON_V5_HINTS
ABAP_HINT_END

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

What does it do?
The function DATS_ADD_MONTHS adds months months to a specified date date.
The actual parameter date must have the built-in data type DATS and should contain a valid date in the format YYYYMMDD.
The actual parameter months must have the built-in data type INT4.
The same applies to the actual parameter on_error as to DATS_ADD_DAYS.
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.

ABAP_EXAMPLE_VX5
The following CDS view entity applies date functions in the SELECT list to columns of the DDIC database table DEMO_EXPRESSIONS. The class CL_DEMO_CDS_DATE_FUNC_VE uses SELECT to access the view. You can assign values to the columns DATN1, DATN2, DATS1 and DATS2 and to the actual parameters for the input parameters. In the case of DATS_ADD_DAYS and DATS_ADD_MONTHS , invalid values or values that produce invalid results are handled as specified by the parameter on_error.
DDLS DEMO_CDS_DATE_FUNC_VE
ABAP_EXAMPLE_END