Skip to Content
Technical Articles

Number of Workdays Between Two Dates with Factory Calendar

Dear Community!!

Recently I came across the requirement to get the number of working days between any two given dates along with the factory Calendar, which needs to be implemented using ABAP CDS view.

After lot of search, I found that one can use the WORKDAYS_BETWEEN function which fits my requirement.
Since this is SQL function and we can’t use the SQL function directly in ABAP CDS views, I built a Table function along with class and method where I can use the SQL functions.

Below is the sample code and output.

Sample Data using ACDOCA –

  1. Used BLDAT and BUDAT as Start and End date.
  2. Used Factory Calendar as CH (Switzerland).
  3. I have hard-coded the Factory Calendar, but you can keep it as a field as per the requirement.

Table Function –>

@EndUserText.label: ‘Table function for Number of Workdays’
define table function ZNumber_of_Workdays
//with parameters parameter_name : parameter_type
returns {
mandt : abap.clnt;
Start_date : abap.dats;
End_date :abap.dats;
WORKDAYS : abap.int1;

}
implemented by method ZCL_WORKING_DAYS=>GET_WORKING_DAYS;

Class & Method –>

 

class ZCL_WORKING_DAYS definition
public
final
create public .

public section.
interfaces if_amdp_marker_hdb.
class-METHODS GET_WORKING_DAYS
for TABLE FUNCTION ZNumber_of_Workdays.
protected section.
private section.
ENDCLASS.

CLASS ZCL_WORKING_DAYS IMPLEMENTATION.
METHOD GET_WORKING_DAYS BY DATABASE FUNCTION
FOR HDB
LANGUAGE SQLSCRIPT
OPTIONS READ-ONLY
using ACDOCA.
RETURN SELECT top 100 DISTINCT
‘100’ AS mandt,
BUDAT as Start_date,
BLDAT as End_date,
WORKDAYS_BETWEEN(‘CH’, BLDAT, BUDAT) as “WORKDAYS” from ACDOCA
where BUDAT > BLDAT ;

ENDMETHOD.
ENDCLASS.

 

Sample Output –

 

 

You can cross check the Workdays against the table TFACS which has counter for the working days of each month.

 

Now you can join the above table function with the required CDS with Start_Date, End_date and Factory Calendar ID (if required) to get the number of Workdays.

This is one way of achieving it in ABAP Using Table function, please share your views and comments based on your experience of achieving the above requirement.

Reference – SAP HANA SQL documentation of WORKDAYS_BETWEEN.

 

Best Regards,

Mayank Jaiswal

 

2 Comments
You must be Logged on to comment or reply to a post.