Concatenate SAP construct in ABAP is used to combine multiple strings into one. There are several options for using this design in SAP systems. In this article I will tell you how to use the old version of string concatenation ABAP and what possibilities of using this construct have appeared in the new syntax ABAP 7.4.

Example Concatenate SAP in old syntax
Sometimes you need to modify old syntax and therefore you need to know how the Concatenate in SAP before 7.4 construct is implemented.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DATA: lv_url TYPE string. " Old ABAP without space CONCATENATE 'ABAP' '-' 'BLOG' '.COM' INTO lv_url. WRITE:/ lv_url. " Result: ABAP-BLOG.COM DATA: lv_title TYPE string. " Old ABAP with space CONCATENATE 'BLOG' 'ABAP' 'FROM' 'ARTUR' INTO lv_title SEPARATED BY space. WRITE:/ lv_title. " Result: BLOG ABAP FROM ARTUR |
Concatenate in new ABAP 7.4
Next we will consider how the ABAP string concatenation design has changed in the new syntax and what new opportunities it gives us. The new ABAP syntax gives more possibilities and adds simplicity and beauty to the code. Whenever possible, I try and recommend using the new ABAP syntax. For example using Concatenate for adding lidding zeros.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
"New ABAP without space DATA(lv_url2) = |ABAP| & |-| & |BLOG| & |.COM| . WRITE:/ lv_url2. " Result: ABAP-BLOG.COM "New ABAP with space DATA(lv_title2) = |BLOG| & | ABAP | & |FROM| & | ARTUR| . WRITE:/ lv_title2. " Result: BLOG ABAP FROM ARTUR DATA(lv_title3) = |BLOG| & | | & |ABAP| & | | & |FROM| & | | & |ARTUR| . WRITE:/ lv_title3. " Result: BLOG ABAP FROM ARTUR " New ABAP syntax using variables DATA(lv_blog) = |BLOG|. DATA(lv_from) = |FROM|. DATA(lv_title4) = |{ lv_blog } ABAP { lv_from } ARTUR|. WRITE:/ lv_title4. " Result: BLOG ABAP FROM ARTUR DATA(lv_title5) = |{ lv_blog } | & |ABAP| & | { lv_from } | & |ARTUR|. WRITE:/ lv_title5. " Result: BLOG ABAP FROM ARTUR |
As a result, we see that using the new syntax is a more concise and understandable notation, the ability to declare a variable at the time of use, and the additional ability to combine spaces when using string concatenation.
New and old Concatenate ABAP
In old syntax Concatenate will be like this:
1 2 3 4 5 6 7 |
DATA: lv_first TYPE string VALUE 'ABAP', lv_second TYPE string VALUE 'BLOG', lv_result TYPE string. CONCATENATE lv_first lv_second INTO lv_result SEPARATED BY space. WRITE: / lv_result. " Output: ABAP BLOG |
In new ABAP syntax concatenate will be
1 2 3 4 5 6 |
DATA(lv_first_new) = 'ABAP'. DATA(lv_second_new) = 'BLOG'. DATA(lv_result_new) = |{ lv_first_new } { lv_second_new }|. WRITE: / lv_result_new. " Output: ABAP BLOG |
Concatenate ABAP in examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
*--------------------------------------------------------------------* DATA(lv_name) = 'Artur'. DATA(lv_age) = 31. DATA(lv_author) = |Name: { lv_name }, Age: { lv_age }|. WRITE: / lv_result. " Output: Name: Artur, Age: 30 *--------------------------------------------------------------------* DATA(lv_de) = abap_false. DATA(lv_site) = 'abap-blog'. DATA(lv_message) = |Website: { lv_site } { COND string( WHEN lv_de = abap_true THEN '.de' ELSE '.com' ) }|. WRITE: / lv_message. " Output: abap-blog.com *--------------------------------------------------------------------* *Logging message DATA(lv_material) = 'MAT123'. DATA(lv_qty) = 5. DATA(lv_log) = |Material { lv_material } posted with quantity { lv_qty }.|. WRITE: / lv_log. *--------------------------------------------------------------------* *Creating keys (e.g. for tables or ALV IDs) DATA(lv_plant) = '1000'. DATA(lv_matnr) = 'MAT001'. DATA(lv_key) = |{ lv_plant }_{ lv_matnr }|. WRITE: / lv_key. " Output: 1000_MAT001 *--------------------------------------------------------------------* *Generating technical names (e.g. for dynamic ALV columns) DO 5 TIMES. DATA(lv_fieldname) = |FIELD_{ sy-index }|. WRITE: / lv_fieldname. ENDDO. *--------------------------------------------------------------------* *Building a URL or file path DATA(lv_server) = 'https://abap-blog.com'. DATA(lv_file) = 'report.pdf'. DATA(lv_url) = |{ lv_server }/downloads/{ lv_file }|. WRITE: / lv_url. *--------------------------------------------------------------------* *Creating dynamic SQL WHERE clause DATA(lv_field) = 'MATNR'. DATA(lv_value) = 'MAT123'. DATA(lv_where) = |{ lv_field } = '{ lv_value }'|. WRITE: / lv_where. *--------------------------------------------------------------------* * Document title generation DATA(lv_docnum) = 'INV1001'. DATA(lv_date) = |{ sy-datum DATE = ISO }|. DATA(lv_title) = |Invoice { lv_docnum } - { lv_date }|. WRITE: / lv_title. *--------------------------------------------------------------------* * Creating a filename with timestamp DATA(lv__file_name) = 'Export'. DATA(lv_time) = |{ sy-datum }_{ sy-uzeit }|. REPLACE ALL OCCURRENCES OF ':' IN lv_time WITH '_'. DATA(lv_filename) = |{ lv__file_name }_{ lv_time }.csv|. WRITE: / lv_filename. *--------------------------------------------------------------------* * User notification (ALV, popup, etc.) DATA(lv_user) = sy-uname. DATA(lv_message_us) = |Hello, { lv_user }! Your report is ready.|. MESSAGE lv_message TYPE 'I'. |
Other articles related to this: