In SAP ABAP CDS Views, you can use several string functions to manipulate and process string data. These functions allow you to perform common tasks like concatenation, trimming, substring extraction, and more.
Below are some common string functions you can use in CDS views:
1. CONCATENATE
The CONCATENATE
function is used to combine two or more strings into one.
Example:
define view ZCONCATENATE_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, concat(carrid, connid) as flight_info}
Explanation:
- The
concat(carrid, connid)
combines thecarrid
(carrier ID) andconnid
(connection ID) fields into one field calledflight_info
.
2. SUBSTRING
The substring
function extracts a portion of a string, based on the starting position and length.
Example:
define view ZSUBSTRING_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, substring(carrid, 1, 2) as short_carrid}
Explanation:
- The
substring(carrid, 1, 2)
extracts the first two characters of thecarrid
field.
3. TRIM
The trim
function removes leading and trailing spaces from a string.
Example:
define view ZTRIM_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, trim(carrid) as trimmed_carrid}
Explanation:
- The
trim(carrid)
removes any leading or trailing spaces from thecarrid
field.
4. UPPERCASE / LOWERCASE
The uppercase
and lowercase
functions are used to convert strings to uppercase or lowercase.
Example:
define view ZUPPERCASE_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, uppercase(carrid) as upper_carrid}
Explanation:
- The
uppercase(carrid)
converts thecarrid
field value to uppercase.
Similarly, you can use lowercase()
to convert the string to lowercase.
5. REPLACE
The replace
function allows you to replace all occurrences of a substring within a string.
Example:
define view ZREPLACE_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, replace(carrid, 'LH', 'LU') as updated_carrid}
Explanation:
- The
replace(carrid, 'LH', 'LU')
replaces all occurrences ofLH
withLU
in thecarrid
field.
6. LENGTH
The length
function returns the length of a string (the number of characters).
Example:
define view ZLENGTH_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, length(carrid) as carrid_length}
Explanation:
- The
length(carrid)
returns the length of thecarrid
field (i.e., the number of characters in the carrier ID).
7. LEFT / RIGHT
The left
and right
functions extract a specified number of characters from the left or right side of a string.
Example:
define view ZLEFT_RIGHT_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, left(carrid, 3) as left_carrid, right(carrid, 3) as right_carrid}
Explanation:
- The
left(carrid, 3)
extracts the first 3 characters from the left of thecarrid
. - The
right(carrid, 3)
extracts the last 3 characters from the right of thecarrid
.
8. CONTAINS
The contains
function checks if a string contains a particular substring.
Example:
define view ZCONTAINS_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, contains(carrid, 'LH') as has_LH}
Explanation:
- The
contains(carrid, 'LH')
returns a boolean value (true
orfalse
) indicating whether thecarrid
contains the substring ‘LH’.
9. POSITION
The position
function returns the starting position of a substring within a string. It returns 0
if the substring is not found.
Example:
define view ZPOSITION_EXAMPLE as select from sflight { key carrid, key connid, fldate, price, position(carrid, 'LH') as position_of_LH}
Explanation:
- The
position(carrid, 'LH')
returns the starting position of the substring ‘LH’ in thecarrid
field. If ‘LH’ is not found, it will return0
.
Conclusion:
In SAP ABAP CDS Views, the following string functions are available to manipulate string data:
- CONCATENATE: To combine multiple strings.
- SUBSTRING: To extract a part of a string.
- TRIM: To remove leading and trailing spaces.
- UPPERCASE / LOWERCASE: To convert strings to uppercase or lowercase.
- REPLACE: To replace substrings within a string.
- LENGTH: To get the length of a string.
- LEFT / RIGHT: To extract characters from the left or right of a string.
- CONTAINS: To check if a string contains a specific substring.
- POSITION: To find the position of a substring within a string.
These functions are useful when you need to format or manipulate data directly within the CDS view before consuming it in your ABAP programs or reports.