CDS View: Add days to Date
This blog is about how to use DATS_ADD_DAYS(date,days,on_error) function when “days” field is not of type INT4.
As part of S/4 HANA Central Finance implementation project, we were converting one of the ECC ABAP program into CDS view and in the ABAP program there was a call to FM NET_DUE_DATE_GET which basically adds days to a date. Here is the calling syntax of this FM:
CALL FUNCTION ‘NET_DUE_DATE_GET’
i_zfbdt = LS_BSID-ZFBDT
i_zbd1t = LS_BSID-ZBD1T
i_zbd2t = LS_BSID-ZBD2T
i_zbd3t = 0
i_shkzg = ‘ ‘
i_rebzg = ‘ ‘
i_koart = ‘D’
e_faedt = Lv_due_date.
Ex-1: LS_BSID-ZFBDT = 13.08.2018, LS_BSID-ZBD1T = 14, LS_BSID-ZBD2T = 0.
Ex-2: LS_BSID-ZFBDT = 13.08.2018, LS_BSID-ZBD1T = 14, LS_BSID-ZBD2T = 20.
Output: 02.09.2018 (if both i_zbd1t and i_zbd2t are passed then FM ignores i_zbd1t and uses i_zbd2t for calculation)
There is a standard CDS function which adds days to passed date DATS_ADD_DAYS(date,days,on_error), unfortunately in this case Days fields i.e. ZBD1T & ZBD2T are not of data type INT4 which is required data type for Date function, Data type of these fields are Dec(3). So if I try to use CDS function on these fields, I will get below error:
So to overcome this Data Type error we need to convert Dec to INT4 and the good thing is CDS has Cast functions which can be used to change the data type of a variable. Ex: cast(gross_amount as abap.fltp) but the not so good thing is Dec cannot be converted into INT4 directly. Well ‘directly’ is the key word here and I found out an indirect way of converting DEC to INT4.
I converted DEC->CHAR->NUMC->INT4 and below table by SAP helped in coming up with this route:
Link where you can find above table:
Here is the final CDS code:
If you have a better solution please share in the comment, suggestions are always welcomed!
BTW if you are looking for CDS date function then here is the link: