Current Date & Time in ABAP CDS Views

On older SAP systems, you may struggle to obtain the current date / time in CDS views.

Passing SY-DATUM and SY-UZEIT as parameters is possible, but not elegant.

Here is my solution: I have created a central CDS View which builds SY-DATUM and SY-UZEIT:

@AbapCatalog.sqlViewName: 'ZBCV_100'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'System values'
define view ZI_BC_SY
  as select from t000 {
  key mandt,
  cast (substring( cast( tstmp_current_utctimestamp()
      as abap.char(17) ), 1, 8 ) as abap.dats) as sydatum,
  cast(substring( cast( tstmp_current_utctimestamp()
      as abap.char(17) ), 9, 6 ) as abap.tims) as syuzeit
}
where
  t000.mandt = $session.client

Whenever I need to use SY-DATUM and SY-UZEIT in a query, I simply INNER JOIN the CDS View above. Case in point:

@AbapCatalog.sqlViewName: 'ZSDV_ACIKSIP_005'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Customer date limit'
@ClientDependent: true
define view ZI_SD_ACIKSP_CUST_DATE_LIMIT
  as select from zsdt_aciksp_002
    inner join   ZI_BC_SY as _sy on _sy.mandt = zsdt_aciksp_002.mandt 
{
  key zsdt_aciksp_002.mandt,
  key vkorg,
  key kunnr,
  dats_add_days(_sy.sydatum, day_back * -1, 'FAIL') as date_limit
}
where
  zsdt_aciksp_002.mandt = $session.client and
  is_active             = 'X'

Neat, eh?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s