Date Functions in ABAP CDS Views

0
5660

Hello everyone,

In this ABAP CDS View tutorial, you will learn how to use Date Functions in ABAP CDS view.

Prerequisites

  • You have installed Eclipse IDE( Mars version )on your local machine.Click here to know more about.
  • You have installed ABAP Development Tools in Eclipse IDE.
  • You have access to minimum ABAP Netweaver 7.5 on HANA.
  • You have created ABAP Project in eclipse to connect to ABAP Netweaver 7.4 system.Click here to know how to create ABAP Project.

[perfectpullquote align=”full” color=”#E5002D” class=”” size=””]Please be note that Date and Time Functions are introduced in SAP NW ABAP 7.5[/perfectpullquote]

Below are the list of Date and Time Functions in ABAP CDS views.

1DATE_IS_VALID(date)

The date function DATE_IS_VALID is used to validate the date contains the valid SAP date format “YYYYMMDD“. It returns “1” if the date is in valid date format else “0“. If the date is blank it returns “0“.

@AbapCatalog.sqlViewName: 'ZCDS_DATE'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Date Functions'
define view zcds_date_functions
            with parameters p_from_date:abap.dats
            as select from snwd_so
 {
  snwd_so.buyer_guid,
  snwd_so.billing_status,
  
  // returns 1 - Valid Date
  // returns 0 - Invalid Date
  DATS_IS_VALID(:p_from_date) as from_date
}

2DATS_DAYS_BETWEEN(date1, date2)

The date function DATS_DAYS_BETWEEN calculates the no of days between the two specified dates, date1 and date2.

@AbapCatalog.sqlViewName: 'ZCDS_DATE'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Date Functions'
define view zcds_date_functions
            as select from vbak
 {
  vbak.vbeln,  //Sales Document
  vbak.auart,  //Sales Document Type
  vbak.audat,  //Document Date
  vbak.vdatu,  //Requested delivery date
  
  DATS_DAYS_BETWEEN(audat, vdatu) as no_of_days
}

3DATS_ADD_DAYS(date, days, on_error)

The date DATS_ADD_DAYS adds days to the specified date date. days should be of type INT4, both negative and positive value for days is allowed.

@AbapCatalog.sqlViewName: 'ZCDS_DATE'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Date Functions'
define view zcds_date_functions
            as select from vbak
 {
  vbak.vbeln,  //Sales Document
  vbak.auart,  //Sales Document Type
  vbak.audat,  //Document Date
  vbak.vdatu,  //Requested delivery date
  
  DATS_ADD_DAYS(vdatu, 10, 'NULL')     as option2,  //add 10 days
  DATS_ADD_DAYS(vdatu, -10, 'NULL')    as option1,  //substract 10 days
  DATS_ADD_DAYS(vdatu, 5, 'FAIL')      as option3,
  DATS_ADD_DAYS(vdatu, 4, 'INITIAL')   as option4,
  DATS_ADD_DAYS(vdatu, 2, 'UNCHANGED') as option5
}

4DATS_ADD_MONTHS(date, months, on_error)

The date function DATS_ADD_MONTHS add months months to the specified date date.

@AbapCatalog.sqlViewName: 'ZCDS_DATE'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'Date Functions'
define view zcds_date_functions 
            as select from vbak
 {
  vbak.vbeln,  //Sales Document
  vbak.auart,  //Sales Document Type
  vbak.audat,  //Document Date
  vbak.vdatu,  //Requested delivery date
  
  DATS_ADD_MONTHS(vdatu, 10, 'NULL')     as option1,  //add 10 months
  DATS_ADD_MONTHS(vdatu, -10, 'NULL')    as option2,  //substract 10 months 
  DATS_ADD_MONTHS(vdatu, 5, 'FAIL')      as option3, 
  DATS_ADD_MONTHS(vdatu, 4, 'INITIAL')   as option4,
  DATS_ADD_MONTHS(vdatu, 2, 'UNCHANGED') as option5
}

 

Congrats! You have successfully learned how to use Date Functions in ABAP CDS Views. Please stay tuned for ABAP CDS Views tutorials. Leave a comment in the below comment section and let us know your feedback.