Date Functions in ABAP CDS Views

1
18006

Hello everyone,

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

Prerequisites

  • 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.

Note: Date and Time Functions are introduced in ABAP 7.50

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

1. DATE_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
}

2. DATS_DAYS_BETWEEN(date1, date2)

This 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
}

3. DATS_ADD_DAYS(date, days, on_error)

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
}

4. DATS_ADD_MONTHS(date, months, on_error)

The date function DATS_ADD_MONTHS add months to the specified 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.

Please feel free to comment and let us know your feedback. Subscribe for more updates.

If you liked it ❤️, please share it! Thanks! 🙏

mail

SAPLearners.com is now on Telegram. Click here to join our channel and stay updated with the latest tutorials and updates.

Like us on Facebook and follow us on Twitter.

Comments are closed.