Application Development and Automation 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: 
Read only

How to convert a timestamp to date/time in CDS View?

Former Member
0 Kudos
26,021

Dear experts,

I would like to convert a timestamp to a date and time within a CDS view.

This is what I tried to do:

Cast (Cast(<timestamp> as abap.dec(8,0)) as abap.dats) as <date>

I also thought about first converting the timestamp into a plain numerical character, then using substring and then casting it to abap.dats. Then its giving me an error that says: "dec can't be converted into numc".

The above written code also throws an error - "function not allowed/ no cast within a cast".

It is quite nasty not being able to write a cast within a cast - how else am i supposed to achieve a date here?

I am looking forward to have your answers!

Thanks and BR

Anja

1 ACCEPTED SOLUTION
Read only

jasmin_gruschke
Product and Topic Expert
Product and Topic Expert
10,009

Hi Anja,
what you try to do is currently not possible, a feature not available in CDS views.
Yet it surely is a feature, ABAP developers would like to see on the feature request list .

Cheers,

  Jasmin

8 REPLIES 8
Read only

jasmin_gruschke
Product and Topic Expert
Product and Topic Expert
10,010

Hi Anja,
what you try to do is currently not possible, a feature not available in CDS views.
Yet it surely is a feature, ABAP developers would like to see on the feature request list .

Cheers,

  Jasmin

Read only

Former Member
10,009

Hi Anja,

Yon can try like this.

cast(substring(cast(creationdatetime as abap.char(32)),1,8) as abap.dats) as creationdate

Regards,

Chandru.

Read only

10,009

Please note that timestamp is considered to be UTC and date is usually your local date, so a time zone specification is necessary for a proper conversion, see for instance ABAP-doc for statement "CONVERT TIME STAMP ..."; and then it is impossible to get the correct result using any combination of cast and substring or the like.

Read only

Former Member
10,009

There is now a function you can use in CDS views:

      tstmp_to_dats( timestamp,

                     abap_system_timezone( $session.client,'NULL' ),

                     $session.client,

                     'NULL' )      as cpudt,

Also there's a vration tstmp_to_tims to get a TIMS value.

I'm seeing this on a NW 752 release; not sure when it became available...

Jim

Read only

0 Kudos
10,009

Thanks Jim! Have you found this in documentation anywhere? I've been unable and was hoping to track down which release we'd need.

Read only

0 Kudos
10,009

Wow... it works...

Read only

0 Kudos
10,009

It works!!
Thank you!

Read only

0 Kudos
10,009

cast(Left(cast(case_attr.create_time as abap.char(20)), 15) as abap.dats ) as create_date,

case_attr is table alias
create_time is field name
create date is final date separated from timestamp
Thanks