ABAP CONCATENATE: Join Strings – Syntax and Alternatives

Category
ABAP-Statements
Published
Author
Johannes

The CONCATENATE statement is used to join the content of two or more character-type data objects (variables, literals) into a single string and store the result in a target variable.

Syntax

CONCATENATE <source1> <source2> ... <sourceN>
INTO <target_variable>
[SEPARATED BY <separator>]
[RESPECTING BLANKS].
" [CONDENSING]. " Obsolete, do not use!
  • <source1> <source2> ... <sourceN>: These are the data objects (variables, constants, or literals) whose contents are to be joined sequentially. They should normally be of type STRING, C, N, D, or T. Other types (e.g., numeric) are usually converted to their string representation before concatenation.
  • INTO <target_variable>: This is the variable that will receive the concatenation result. The previous content of this variable is overwritten.
    • Ideally, <target_variable> is of type STRING, as the length then adjusts dynamically.
    • If <target_variable> is of type C (fixed-length string) and the concatenation result is longer than the defined length of the target variable, the result is truncated on the right.
  • SEPARATED BY <separator> (Optional): This allows specifying a separator (e.g., a space space, a comma ',', a hyphen '-') that is inserted between the individual source elements. <separator> is itself a character-type data object.
  • RESPECTING BLANKS (Optional): This addition affects how trailing blanks of source fields with fixed length (type C, N, D, T) are handled.
    • Without RESPECTING BLANKS (default behavior): Trailing blanks in fixed-length fields are removed before concatenation.
    • With RESPECTING BLANKS: Trailing blanks in fixed-length fields are preserved.
    • Important: For source fields of type STRING, trailing blanks are always considered, regardless of this addition. Therefore, RESPECTING BLANKS is usually only relevant when you explicitly want to preserve the original length of type-C fields including their blanks (and the target might also be type C).

Modern Alternatives (Preferred!)

In modern ABAP (version 7.40 and higher), there are much more flexible and often more readable alternatives to CONCATENATE:

  1. String Templates (literals with |...|): Allow direct embedding of variables and expressions in a string.
    DATA(lv_name) = 'World'.
    DATA(lv_greeting) = |Hello { lv_name }! It is { sy-timlo }.|.
  2. Concatenation Operator (&&): Directly joins two strings.
    DATA(lv_part1) = 'Part 1'.
    DATA(lv_part2) = 'Part 2'.
    DATA(lv_combined) = lv_part1 && ' and ' && lv_part2.

It is strongly recommended to use string templates or the && operator for new developments! However, CONCATENATE is important for understanding and maintaining older code.

Examples for CONCATENATE

1. Simple Concatenation

DATA: firstname TYPE string VALUE 'Anna',
lastname TYPE string VALUE 'Smith',
fullname TYPE string.
CONCATENATE firstname lastname INTO fullname.
WRITE: / fullname. " Output: AnnaSmith

2. Concatenation with Separator

DATA: city TYPE string VALUE 'Berlin',
zip_code TYPE string VALUE '10117',
address TYPE string.
" Separate with space
CONCATENATE zip_code city INTO address SEPARATED BY space.
WRITE: / address. " Output: 10117 Berlin
" Separate with comma and space
CONCATENATE city zip_code INTO address SEPARATED BY ', '.
WRITE: / address. " Output: Berlin, 10117

3. Concatenation of Different Data Types

DATA: item_count TYPE i VALUE 5,
item_desc TYPE string VALUE 'Apples',
output_str TYPE string.
CONCATENATE item_count item_desc 'available' INTO output_str SEPARATED BY space.
WRITE: / output_str. " Output: 5 Apples available

(The number 5 is automatically converted to a string '5').

4. Handling Blanks with Type C (with and without RESPECTING BLANKS)

DATA: field_c1 TYPE c LENGTH 10 VALUE 'Text One', " Has 1 blank at the end
field_c2 TYPE c LENGTH 10 VALUE 'Two'. " Has 6 blanks at the end
target_str TYPE string.
" Default: Trailing blanks from field_c1 and field_c2 are removed
CONCATENATE field_c1 field_c2 INTO target_str SEPARATED BY '|'.
WRITE: / target_str. " Output: Text One|Two
" With RESPECTING BLANKS: Trailing blanks are preserved
CONCATENATE field_c1 field_c2 INTO target_str SEPARATED BY '|' RESPECTING BLANKS.
WRITE: / target_str. " Output: Text One |Two |

5. Comparison with String Template

" Equivalent to example 2
address = |{ zip_code } { city }|.
WRITE: / address. " Output: 10117 Berlin
" Equivalent to example 3
output_str = |{ item_count } { item_desc } available|.
WRITE: / output_str. " Output: 5 Apples available

Important Notes / Best Practice

  • CONCATENATE is the classic method for string concatenation in ABAP.
  • Be aware of the default behavior regarding trailing blanks for fixed-length fields (like type C).
  • Ideally use STRING as the target type to avoid truncation.
  • For new code, prefer string templates (|...|) or the concatenation operator (&&), as these are usually more readable, flexible, and less error-prone.