CDS Part 8. Usage of Built-In Functions in CDS – I

Built-In Functions in Code Data Services has been addressed pretty late here at SAPSPOT. Our apology for the delay. Probably we might have underestimated the usage of this powerful function. Nonetheless, better late than never. Let’s try to grasp the concept.

CDS View Functions are present to help increase the throughput of the developer by minimizing the efforts to make functionality work.

Did it ring any bell? No. Please continue below for more clarity.

There are two kinds of functions available.

1. SQL Functions
2. Built In Functions
• SQL Functions

These are kind of functions which essentially are make or break. They are not flexible enough to give you a third option. Simply put they return LOGICAL or BOOLEAN result.

Sub Species are there as usual.

1. Numeric Functions

 FUNCTION DEFINITION OUTPUT ABS(arg) In mathematics, the absolute value or modulus |x| of a real number x is the non-negative value of x without regard to its sign Absolute amount CEIL(arg) Hitting the Ceiling of the Floating Number. Smallest integer number not less than the value of arg DIV(arg1, arg2) Conventional Division Quotient DIVISION(arg1, arg2, dec) Conventional Division but with an additional feature of specifying decimal places The result is rounded to dec decimal places. MOD(arg1, arg2) Conventional Modulo Operation Remainder FLOOR(arg) Largest integer number not greater than the value of arg. More like scientific numbers ROUND(arg, pos) Rounded value of arg. Rounding the Designated decimal point value

Below code snippet on ABS, CEIL, FLOOR, DIV, DIVISION, MOD and ROUND should help you understand the concept better.

``````@AbapCatalog.sqlViewName: 'ZFLIGHT_SCH_V'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFLG_FNC_DDL as select distinct from sflight as a
{
abs(-2) as Abs_Op,

/*Ceil and Floor*/
ceil(25.3) as Ceil_Op,
floor(25.3) as Floor_Op,

/*Division*/
div(5,3) as Div_Op,
division(5,3,5) as Div_Op2,
mod(5,3) as Mod_Op,

a.price as Flg_Price,
round( a.price,1) as Round_Op
}``````

Please check each output. Did you notice Ceil is 26 for a number 25.3 while Floor is 25 for the same number 25.3.

2. String Functions

SAP has been very kind to String somehow. Poor Numbers.

Let’s not talk much but let the code do the talking here.

``````@AbapCatalog.sqlViewName: 'ZSTR_FN_V1'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFNC_DDL as select distinct from makt as a
{
key a.matnr as Mat_Num,
a.maktx as Mat_Desc,
length( a.maktx ) as Des_Len,
instr( a.maktx, 'est' ) as Des_Find,
concat( a.maktx, a.spras ) as Des_Con,
concat_with_space( a.maktx, a.spras, 2 ) as Des_Con_space,
left( a.maktx, 3 ) as Des_left,
lower( a.maktx ) as Des_lower
}``````

Part 1 of Strings:

 Function Description Length gives Length of String Instr finds the position of a respective string within corresponding the field of the View Concatenate joining two strings Concatenate with Space The third Parameter in this function represents the Number of space between two strings Left gives us left most characters equal to argument passed Lower converts all into lower case [ Rather Subscript ]

Now, wondering where are the complementing string functions??? They are below.

``````@AbapCatalog.sqlViewName: 'ZSTR_FN_V1'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFNC_DDL as select distinct from makt as a
{
key a.matnr as Mat_Num,
a.maktx as Mat_Desc,
right( a.maktx, 5 ) as Des_right,

/*For Strings */
ltrim( a.maktx, 't' ) as Des_ltrim,
rtrim( a.maktx, 't' ) as Des_rtrim,
replace( a.maktx, 'est','ough' ) as Des_replace,
substring( a.maktx, 2, 1 ) as Des_substring,
upper( a.maktx ) as Des_upper
}``````

Check how conveniently you can play around with the strings using lpad, rpad, ltrim, rtrim, replace, substring, upper etc. They come really handy in actual projects.

ABAPers !!! How on this earth can we forget our traditional methods working with Conversion FMs ??? Isn’t it???

Well, those are replaced by Padding and Trimming – Both sides Left and Right.

 Function Description Lpad & Rpad the first parameter is field, second is the OUTPUT Length after padding, a string that is to be padded Ltrim & Rtrim the first parameter is field, second is string or character that is to be removed Replace the second parameter finds the string to be replaced by the third Substring The third Parameter in this function represents the Number of space between two strings Upper converts all characters of string into Upper case

Points to Remember:

a. In Padding, the Output Length needs to be greater than Length of existing field to enable padding. Else, we will have a trimmed output.

b. Padding and Trimming can be used for Numerical Values as well.

c. Only a Single Character needs to be given for trimming.

``````Example:
define view ZFLG_FNC_DDL as select distinct from sflight as a
{
key a.connid as Flg_Connid,

/*For Numericals */
ltrim( a.connid, '0' ) as Flg_ltrim,
a.fldate as Flg_Date,
rtrim( a.fldate, '8' ) as Flg_rtrim
}``````

Bonus Segment:

Requirement : “Change the data retrieved for Flight Time from Total Minutes to Hours:Minutes Format”

How about complicating our lives? Actually, it may be the way to simplify. Let’s give a try.

Initial Code and Output :

``````@AbapCatalog.sqlViewName: 'ZFLIGHT_SCH_V'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Built In Functions'
define view ZFLG_FNC_DDL as select from spfli as a
left outer join sgeocity as b
on a.cityfrom = b.city
{
key b.city as Source,
key b.city as Destination,
key a.carrid as Flg_ID,
key a.connid as Flg_Conn,
a.fltime as Flg_Time
}``````

Functions can be handy here…Check out the code below which would suffice our requirement.

``````@AbapCatalog.sqlViewName: 'ZFLIGHT_SCH_V'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@EndUserText.label: 'Flight Schedule Details'
define view ZFLG_FNC_DDL as select from spfli as a
left outer join sgeocity as b
on a.cityfrom = b.city
{
key b.city as Source,
key b.city as Destination,
key a.carrid as Flg_ID,
key a.connid as Flg_Conn,
concat( concat(lpad ( ltrim ( cast( div(a.fltime, 60) as abap.char( 12 ) ), '0' ), 2, '0' ), ':' ) ,
lpad ( ltrim ( cast( mod(a.fltime, 60) as abap.char( 12 ) ), '0'), 2, '0' ) ) as Flg_Time
}``````

The above one line string function is just an example of how you can play around with any requirement. Break the code from center and go outward. It is self-explanatory.