CDS Part 16. Usage of Built-In Functions in CDS – IV

In our Built In Functions in CDS we have covered SQL Functions, Unit/Currency Conversion Functions, Date Functions and we fell short of Time function. Today we will start where we left in the last post. Let us jump into what might be the last edition in this series of Built-In Functions.

CDS Part 10. Usage of Built-In Functions in CDS – III

In this post let’s traverse through the Time Functions. The 4 functions listed below shall be very handy to maneuver with Time.

    Single Parameter needs to be passed. The field type should and must be TIMESTAMP – DEC – Length 15. Same goes with the rest of the functions in this blog.
    Best part of this is there is no parameter that needs to given. By default returns us the Coordinated Universal Time (UTC).
    As name suggests we add the seconds to the timestamp and the output is the sum of the numbers.
    Remember our last edition had DAYS BETWEEN. Now here we have seconds between.

Customs are not to be broken. How can we not provide the Code and sample output?

<code>@AbapCatalog.sqlViewName: 'ZTM_FNS_SQL_V'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Time Functions'
define view ZTM_FNS as select from zdt_concr_rept as a{
    key a.rpt_id as RPT_Comment,
    a.ztime as RPT_TMSP,
    tstmp_is_valid(a.ztime) as valid1,   
    tstmp_current_utctimestamp() as UTC_TM,
    tstmp_add_seconds(a.ztime, cast( 15 as abap.dec(15,0) ), 'INITIAL') as ADDED_TM,    
    tstmp_seconds_between(tstmp_current_utctimestamp(), a.ztime , 'FAIL') as difference 

Nothing that needs special mention .

If this is not interesting, we have a bonus segment. This segment shall connect to different worlds : SAP and all other.

OData: Many of you have heard and worked on this beautiful concept. But how about connecting CDS with outer world (other than SAP) through OData service??

Here you go:

<code>@AbapCatalog.sqlViewName: 'ZTYPE_CAST_V'
 @AbapCatalog.compiler.compareFilter: true
 @AbapCatalog.preserveKey: true
 @AccessControl.authorizationCheck: #NOT_REQUIRED
 @EndUserText.label: 'Type Casting Examples'
 define view ZTYPE_CAST
   as select from sflight as a
   key a.carrid,
   key a.connid as originalConn,
   key a.fldate,
       cast (a.connid as abap.char(2) )
       as castedConn,
       a.planetype as originalType,
       cast( a.planetype as abap.char( 2 ) )
                   as castedType

Observe keenly there are two warnings. Let’s see what are they.

So effectively two things needs to be kept in mind.

  1. Apply Type casting only when changing from one data type to other. If it is done within same data type the very purpose is not served.
  2. Need to be very careful such that no harm is done to data.

Output justifies the claim made earlier

Another good learning for me !!! I tried to put non primary key castedConn before fldate. But SAP did not allow me.