sankar babu
sankar babu

Reputation: 1

How to get beginning/start date and month of the year in ABAP CDS view using $session.system_date

The requirement is to fetch all the material master data based on the date and the date must be beginning of the year i.e., 01.01.2024. We have declared $session.system_date as a current date input but the new requirement is to change from the beginning.

The below one can be used for yesterday? but need help to derive the beginning or first date of the year and first month of the year. p_startdate : abap.dats $session.system_date as today, DATS_ADD_DAYS( :p_startdate,-1,'NULL') as yesterday

Is there a way to pick the year from $session.systemdate and concatenate to 01.01. in CDS? or please share if there is any other way to meet this requirement.

Our version is SAP_BASIS 756 0001 SAP_ABA 75G 0001

Thanks in advance.

No other input then a $session.system_date as today. So I don't know how to write to get the first day and first month from the $session.system_date.

Upvotes: -1

Views: 1189

Answers (2)

Zero
Zero

Reputation: 1646

It's not going to be pretty (at least 4 nested operations), but a fairly reliable way to do it is effectively as follow.

  • Let's say our date is 2024/07/24
  • First day of the month: concat( Substr( <date>, 1, 4 ), '01' ): 2024/07/01
  • Next month: dats_add_month( <date>, 1 , 'UNCHANGED') : 2023/08/01
  • Last day of current month = first day of next month - 1 day dats_add_day( <date>, -1 , 'UNCHANGED') 2024/07/31

Note that you might need to cast around concat because it returns char, which other functions don't accept.

Also swap out the UNCHANGED for other error handling if needed.

I'm not 100% it works in parameters, because Abap CDS is picky about some things, so if it doesn't see if you can get some date from your query and work with that.

Upvotes: 0

mkysoft
mkysoft

Reputation: 5758

You can use some string operations with cast like below:

@AbapCatalog.sqlViewName: 'ZYOUR_CDS_VIEW'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'CDS View to Get First Date of Current Year'
define view ZYour_CDS_View as select from some_table {
    some_field,
    CAST( CONVERT( CONVERT( substring( 'YYYY', 1, 4 ) || '-01-01', 'YYYY-MM-DD' ), 'DATS' ) AS DATS ) as First_Date_Of_Current_Year
}

Upvotes: 0

Related Questions