3

Is there any way to get current timestamp or current date ? SQL syntax is date.now() but it doesn't work in ABAP CDS. Is there any solution without parameters ?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Mert
  • 474
  • 2
  • 8
  • 21

2 Answers2

3

In 7.50 you have tstmp_current_utctimestamp(). It may be used to compare with other timestamps, leading to a need to convert typical date and time fields. Example:

// As our system is set to UTC already, these cast and calculation are OK awaiting ABAP 7.51. Add a day if time is 24:00.
case resb.bdztp when '240000' 
                then cast( cast( cast( concat( DATS_ADD_DAYS( resb.bdter, 1, 'NULL'), '000000' ) as abap.numc(14) ) as abap.dec( 15, 0 ) ) as timestamp )
                else cast( cast( cast( concat( resb.bdter, resb.bdztp )                          as abap.numc(14) ) as abap.dec( 15, 0 ) ) as timestamp )
end as RequirementDateTimeUTC,

Consumption:

// Seconds since Requirement Date & Time for view isOverdue. 
tstmp_seconds_between( resb.RequirementDateTimeUTC, tstmp_current_utctimestamp(), 'NULL') as SecondsSinceReqDateTimeUTC,
Mikael G
  • 712
  • 5
  • 13
  • Thanks for this! I can harldy image figuring out such a casting concatenation cascade on my own. Did you find these workarounds/hacks in an official documentation or did you develop using trial and error? – koks der drache Nov 05 '19 at 16:54
  • thanks for that. but my abap 7.40 :( is that any solition for 7.40 ? or hard to upgrade for 7.50 ? – Mert Nov 06 '19 at 06:27
  • 1
    @konstantin: Inspiration for the cast from here: https://blogs.sap.com/2019/04/11/cds-view-add-days-to-date/ – Mikael G Nov 06 '19 at 12:33
  • 1
    @Mert: For 7.40, I'm not aware of any solution. – Mikael G Nov 06 '19 at 12:36
1

The session variable $session.system_date is used in a CDS view to provide direct access to the current system date.

There is not yet a session variable for the current system time and a CDS view can be given an appropriate input parameter instead. The special annotation @Environment.systemField makes it possible to pass the value of the ABAP system field sy-uzeit to a parameter of this type.

Source

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
gbshahaq
  • 11
  • 1
  • 1
    Please carefully check your ABAP version. There have been quite a lot of changes in the CDS between [7.50](https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-us/abencds_f1_date_time_functions.htm) and 7.51. For example using the current datetime in `WHERE` clauses or `JOIN` conditions is not possible in ABAP 7.50. – koks der drache Nov 05 '19 at 13:01
  • @konstantin, what's wrong with this answer? It's OK. Session variables haven't changed since 7.50 to 7.51 and neither OP nor answerer didn't mention the need of`WHERE`/`JOIN`. Don't fabricate additional restrictions for simple question – Suncatcher Nov 08 '19 at 02:35
  • @Suncatcher : In my case using `$session.system_date` in the SELECT list results in an "ABAP Activation problem" during activation of the CDS definition in ABAP 7.50: "Unexpected word '$session.system_date'" I can only use a workaround and inject it as a parameter: `with parameters @Environment.systemField : #SYSTEM_DATE p_datum : syst_datum` (with a lot of performance side effects). See also [ABAP News for Release 7.51 – Date and Time in ABAP CDS and Open SQL](https://blogs.sap.com/2016/11/04/abap-news-for-release-7.51-date-and-time-in-abap-cds-and-open-sql/) by Horst Keller. – koks der drache Nov 08 '19 at 07:17
  • 1
    See also [this answer by Horst Keller on the exact same question](https://answers.sap.com/answers/12547821/view.html): _"Up to Release 7.50, there is no session variable for the system date in ABAP CDS. This will only come with an upcoming release (7.51). Before 7.51, you can use a parameter for that. See the discussion Current date in ABAP CDS views | SCN. If you are working on 7.51 already, use $session.system_date."_ – koks der drache Nov 08 '19 at 07:34
  • Yes, the date variable appeared in the 7.51, but as the version is not specified explicitly in the question, the answers should be version-agnostic and suggest all-versions solutions. So this answer is perfectly valid if anybody find this question further – Suncatcher Nov 08 '19 at 20:39