Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Current date in ABAP CDS views

anita_dixit
Advisor
Advisor
0 Kudos
33,554

Hi,

I am trying to get current system date into a variable (in the form of parameter) as shown below :

What I need is  : If the end date is less than today's date, it's fine(continue with the end date)  else replace it with today's date (that's get it from the system's date) . But I am getting error such that it is not supported.

Can anyone suggest a workaround solution at the earliest ?

Thanks & Kind Regards,

Anita

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
5,190

Hi Anita,

Unfortunately, result1, result2, ... of a CASE expression cannot be parameters (up to now).

But, hey, they can be CAST expressions. Therefore, a cheap trick that gives no error in 7.50:

@AbapCatalog.sqlViewName: 'KELLERH_V_TEST'

define view kellerh_cds_test

  with parameters

    @Environment.systemField: #SYSTEM_DATE

    p_start_date : abap.dats

  as select from demo_expressions

{

  dats1,

  dats2,

  case when dats2 < $parameters.p_start_date

    then dats2

    else cast( $parameters.p_start_date as abap.dats )

  end as res

}

Cheers

Horst

10 REPLIES 10

michaelwurst
Associate
Associate
0 Kudos
5,190

Hi Anita,

I think one (dirty) workaround in your scenario could be to define a second CDS view with identical interface as wrapper and use the parameter as colum output. Maybe you can try something like this (untested):

1. Adjust your CDS view by adding a column with system date, e.g.:

define view ZEMP_YEARS_1

with parameters

@Environment.systemField: #SYSTEM_DATE

@EndUserText.label: 'Test'

p_start_date : abap.dats

as select from ZAD_EMPSTAFFING{

  -- your column fields... 

  $parameters.p_start_date as sydatum    

}

2. Create new CDS view, e.g.:

define view ZEMP_YEARS_1_WRAP

with parameters

@Environment.systemField: #SYSTEM_DATE

@EndUserText.label: 'Test'

p_start_date : abap.dats

as select from ZEMP_YEARS_1( p_start_date: $parameters.p_start_date ){

  -- your column fields... 

  case

    when ZEMP_YEARS_1.EndDate < ZEMP_YEARS_1.sydatum then ZEMP_YEARS_1.EndDate

    else ZEMP_YEARS_1.sydatum end as EndDateU     

}

I hope this helps and all the best,

Michael

0 Kudos
5,190

Thanks. I did it long time back.

horst_keller
Product and Topic Expert
Product and Topic Expert
5,191

Hi Anita,

Unfortunately, result1, result2, ... of a CASE expression cannot be parameters (up to now).

But, hey, they can be CAST expressions. Therefore, a cheap trick that gives no error in 7.50:

@AbapCatalog.sqlViewName: 'KELLERH_V_TEST'

define view kellerh_cds_test

  with parameters

    @Environment.systemField: #SYSTEM_DATE

    p_start_date : abap.dats

  as select from demo_expressions

{

  dats1,

  dats2,

  case when dats2 < $parameters.p_start_date

    then dats2

    else cast( $parameters.p_start_date as abap.dats )

  end as res

}

Cheers

Horst

0 Kudos
5,190

Thanks a lot . It worked.

Former Member
5,190

Hello,

Is there any way to do arithmatic on date and time fields?

would you pls let me know syntax.

normal CDS view or Query type CDS view (@Analytics.query: true)

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos
5,190

Since 7.50 there are built-in functions for this.

0 Kudos
5,190

Hi Saif,

I am using normal CDS view (dataCategory: #CUBE).

former_member597044
Participant
0 Kudos
5,190

Hi Experts,
Without using Parameters , do we get the system field in Plain CDS View.

Thanks
Sumanth

5,190

Hi Sumanth,

Maybe, it can help.

"tstmp_to_tims(TSTMP_CURRENT_UTCTIMESTAMP(), abap_system_timezone( $session.client,'NULL' ), $session.client, 'NULL') as SYSTIME ,"

Kind Regards,

Carlos

dob1
Participant
5,190

I know this question is old, but I just had the same "challenge".

It depends on the release you are working on.

Release 7.51 and higher provides a session-variable that gives us directly the current date.

$session.system_date

https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-us/abencds_f1_session_variable.htm