ABAP CDS View Tip: First and Last Day of Month

Here are two cool formulas to calculate the first & last days of a given month.

First day:

cast( concat( concat( year, month ), '01' ) as abap.dats ) as first_day

Last day:

DATS_ADD_DAYS(

  DATS_ADD_MONTHS(

    cast( concat( concat( year, month ), '01' ) as abap.dats ),

    1, 'FAIL'),

  -1, 'FAIL'
) as last_day

Real world example:

@AbapCatalog.sqlViewName: 'ZMMV_015'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Open plant period'

define view ZI_MM_OPEN_PLANT_PERIOD as 
select from t001w
inner join t001k on t001k.bwkey = t001w.bwkey
inner join marv on marv.bukrs = t001k.bukrs {
key t001w.werks,
marv.vmgja,
marv.vmmon,
marv.lfgja,
marv.lfmon,

cast( concat( concat( marv.vmgja, marv.vmmon ), '01' ) as abap.dats ) as first_day,

DATS_ADD_DAYS(
  DATS_ADD_MONTHS(
    cast( concat( concat( marv.lfgja, marv.lfmon ), '01' ) as abap.dats ),
    1, 'FAIL'),
  -1, 'FAIL')
as last_day

}

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