How to use String Functions in ABAP CDS Views

0
49870

Hello everyone,

In this ABAP CDS view tutorial, you will learn how to use string functions in ABAP CDS Views.

Also Read:  All ABAP CDS Views tutorials. Lets get started.

Prerequisites

  • You have installed Eclipse IDE( Mars Version )on your local machine.Click here to know more about.
  • You have installed ABAP Development Tools in Eclipse IDE.
  • You have access to minimum ABAP Netweaver 7.4 on HANA.
  • You have created ABAP Project in eclipse to connect to ABAP Netweaver 7.4 system.Click here to know how to create ABAP Project.

String Functions in ABAP CDS Views

Below is the list of frequently used string functions in ABAP CDS views.

1CONCAT(arg1, arg2)

CONCAT(arg1, agr2) string function can be used to concatenate two character strings.

@AbapCatalog.sqlViewName: 'ZCDS_STR_FUN'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'String Functions'
define view Zcds_Sql_Func as select from kna1 {

    // CONCATENATE name1 & name2 
    CONCAT( kna1.name1, kna1.name2 ) as full_name
}

2CONCAT_WITH_SPACE(arg1, arg2, spaces)

This string function is used to concatenate two character strings with space. The number of blanks between the arguments arg1 and arg2 is specified in spaces.

@AbapCatalog.sqlViewName: 'ZCDS_STR_FUN'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'String Functions'
define view Zcds_Sql_Func as select from kna1 {

    // CONCATENATE name1 & name2 with 4 space
    CONCAT_WITH_SPACE( kna1.name1, kna1.name2, 4 ) as full_name
}

3SUBSTRING(arg, pos, len)

To get sub string of arg from the position pos in the lenght len.

@AbapCatalog.sqlViewName: 'ZCDS_STR_FUN'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'String Functions'
define view Zcds_Sql_Func as select from kna1 {
    
    // To get substring for a given string
    SUBSTRING( kna1.name1, 2, 10) as name
}

4LENGTH(arg)

It returns the no of characters in the string which is passed as a argument arg. It ignores trailing blanks.

@AbapCatalog.sqlViewName: 'ZCDS_STR_FUN'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'String Functions'
define view Zcds_Sql_Func as select from kna1 {
    
    // To get length for a given string
    LENGTH( kna1.name1 ) as name_length
}

5LEFT(arg, len) & RIGHT(arg, len)

LEFT(arg, len) – It returns the left-side part of the string which is passed as argument arg of length len.

RIGHT(arg, len) – It returns the right-side part of the string which is passed as argument arg of length len.

Note: Trailing blanks are ignored

@AbapCatalog.sqlViewName: 'ZCDS_STR_FUN'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'String Functions'
define view Zcds_Sql_Func as select from kna1 {
    
    // To get length for a given string     
    LEFT( kna1.name1, 3) as name_left,
    RIGHT( kna1.name1, 3) as name_right,
    kna1.name1
}

6LTRIM(arg, char) & RTRIM(arg, char)

LTRIM(arg, char) – It removes the trailing blanks and leading character which matches the parameter char.

RTRIM(arg, char) – It removes the trailing blanks and trailing character which matches the parameter char.

@AbapCatalog.sqlViewName: 'ZCDS_STR_FUN'
@AbapCatalog.compiler.compareFilter: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'String Functions'
define view Zcds_Sql_Func as select from kna1 {
    
    // Removes the trailing blanks and character 
    LTRIM( kna1.name1, 'L') as name_lt,
    RTRIM( kna1.name1, 'T') as name_rt    
}

 

Congrats! You have successfully learned String Functions in ABAP CDS Views.Please stay tuned for more ABAP CDS view tutorials.

Leave a comment in the below comment section and let us know your feedback.