Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CDS Usage of Built-In Functions in CDS – I

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

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.

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

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

3.

@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 respective
string within corresponding the
field of the View
Concatenate joining two strings
Concatenate with Third Parameter in this function
Space 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 */
lpad( a.maktx, 6, 'xx' ) as Des_lpad,
rpad( a.maktx, 6, 'y' ) as Des_rpad,
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. What about Our traditional
methods working with Conversion FMs ? Well, those are replaced by Padding and Trimming –
Both sides Left and Right.
Function Description
Lpad & Rpad first parameter is field, second
is the OUTPUT Length after
padding, string that is to be
padded
Ltrim & Rtrim first parameter is field, second
is string or character that is to
be removed
Replace second parameter finds the
string to be replaced by the
third
Substring finds the string that you want –
second parameter is starting
position and third is how many
characters
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 */
lpad( a.connid, 6, '22' ) as Flg_lpad,
rpad( a.connid, 6, '99' ) as Flg_rpad,
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
}

You might also like