Home > Back-end >  Calculate previous day in ABAP CDS?
Calculate previous day in ABAP CDS?

Time:10-18

I am starting with ABAP, I use the following instruction to obtain the date in the system but I can't get the date of the previous day, does anyone have any suggestions? THANKS.

I tried with:

 { 
  $session.system_date as today,
  $session.system_date - 1 as yesterday
 }

but I can't do arithmetic operations with dates and it marks an error.

CodePudding user response:

You can use DATS_ADD_DAYS function for this task.

It does not allow putting session vars directly as an argument, but we can do the following trick:

define view Z_TODAY
  as select from 
    zsomething
    { 
      key field1 as type1,
      key field2 as type2,
          ...
          $session.system_date as today,
          DATS_ADD_DAYS( cast( $session.system_date as abap.dats ),-1,'NULL') as yesterday
    }

Wrapping the system date into CAST does the thing.

Another, less intuitive way (which can be more flexible sometimes) is putting optional parameter with predefined value:

define view Z_TODAY
with parameters  
  @Environment.systemField : #SYSTEM_DATE 
  p_datum : abap.dats
  as select from zsomething
    { 
      key field1 as type1,
      key field2 as type2,
          ...
          $session.system_date as today,
          DATS_ADD_DAYS( cast( $session.system_date as abap.dats ),-1,'NULL') as yesterday
    }
  • Related