String functions in general used to perform manipulation of string data. In ABAP CDS there are a variety of string functions to perform operation in the strings. These functions have certain limitations on how the operations can be done. The details are explained in the example below.
For this illustration, we have created a table named ZSTRINGDATA to consume in CDS views. The table contains ID and ID_DESC fields as shown in the image below
Below data is loaded into the table to illustrate the string operations capabilities in CDS views. Where Customer Denotes the sales person and description denotes the file they have shared which contains area codes denoting from “_” to “.”.
String operations in ABAP CDS views contains variety of functions like CONCAT, LTRIM, RTRIM, LEFT, RIGHT, SUBSTRING etc., which can be used to manipulate the string values form the data. For more details on the string functions refer to the ABAP documentation link provided below:
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abencds_f1_sql_functions_character.htm
In this blog we are going to discuss more about the string functions like LEFT(arg, len), RIGHT(arg,len), SUBSTRING(arg, pos, len) where the length has been specified to fetch data.
In the below CDS view the functions left, right and substring provides data based on the length specified.
Output:
Let’s check the case where we need to fetch the area codes dynamically from description(ID_DESC) using the syntax <NAME>_<AREA_CODE>.<EXTN>form “_” to “.”.
The position of these are being calculated using the string function instr and this function provides integer as an output.
Instr(“ID_DESC”,’_’) to fetch the position of character “_”
Instr(“ID_DESC”,’.’) to fetch the position of character “.”
Ideally since the instr function provides integer data type as output it can be used in the string functions to fetch the data from a specific length.
When this function is used in other functions with length to be provided it shows error that only literals, possibly positive numbers must be used in the function. The details are provided in the image shown below.
Error message is shown in the below image:
To resolve the above issue, we would have to approach the solution from other perspectives namely:
- Extended view based on HANA Views- View blog here
- ABAP CDS Table Function with AMDP Function Implementation- View blog here