Concatenating a space in CDS views

Lilienthal picture Lilienthal · Dec 23, 2016 · Viewed 7.2k times · Source

As of ABAP 7.40 SP5 I can use the CONCAT function to combine two fields in my CDS view select list. It's limited to two parameters but I can work around that by chaining this function to combine multiple fields or build larger strings. What I can't do this way is combine two fields with a space separating them. When I do this:

define view Z... 
as select from but000 as bp
{
    concat( concat( bp.name_first, ' '), bp.name_last )
}

The space ' ' is silently trimmed from the resulting string. How can I separate fields with a space?

Answer

Lilienthal picture Lilienthal · Dec 23, 2016

ABAP 7.50

ABAP 7.50 will include the CONCAT_WITH_SPACE function that addresses this problem. With that function the example above can be written simply as:

CONCAT_WITH_SPACE( bp.name_first, bp.name_last, 1 )

With the 1 referring to the number of spaces that are to be inserted between the two arguments.

7.50 also introduces other string functions like INSTR, LEFT, LENGTH, LTRIM, RIGHT, RPAD and RTRIM. 7.51 looks set to add LOWER and UPPER to that list.


ABAP 7.40

There is no clean way to accomplish the same in this release. The only way seems to be to concatenate the two fields with a dummy string that encloses the space in a character group that won't appear in the fields that are being selected. After combining you can then remove those characters from the result, leaving just the space. I've taken this approach from Christian Seitel on the SAP forums.

REPLACE(CONCAT( CONCAT( bp.name_first, '|-| |-|'), bp.name_last),'|-|', '')

This works because it will process this string as follows:

name_first|-| |-|
name_first|-| |-|name_last
name_first name_last