ABAP string templates create readable text without long chains of CONCATENATE statements. A template is enclosed by pipe characters and can mix literal text with embedded expressions inside braces.
1. Your first string template
DATA(lv_message) = |Hello { sy-uname }!|.
DATA(lv_order_text) =
|Order { lv_vbeln } belongs to customer { lv_kunnr }.|.Everything outside braces is literal text. Each expression inside { ... } is evaluated and converted to character form at runtime.
2. Classic concatenation versus modern syntax
"Classic
CONCATENATE 'Order' lv_vbeln 'is ready'
INTO lv_text SEPARATED BY space.
"Modern
DATA(lv_text_new) = |Order { lv_vbeln } is ready|.The template makes spaces, punctuation and field positions visible in one place. This reduces formatting defects during maintenance.
3. Dates, times and numbers
DATA(lv_summary) =
|Date: { sy-datum DATE = USER } |
&& |Time: { sy-uzeit TIME = USER } |
&& |Amount: { lv_amount NUMBER = USER DECIMALS = 2 }|.DATE = USER, TIME = USER and NUMBER = USER follow the current user formatting environment. Use an explicit technical format when a machine-readable interface requires a stable representation.
4. ALPHA conversion in a template
DATA(lv_display) = |Sales order { lv_vbeln ALPHA = OUT }|.
DATA(lv_internal) = |{ lv_input ALPHA = IN }|.ALPHA = OUT removes leading zeros for display, while ALPHA = IN adds them when the value and target context support the conversion. Validate domain lengths before relying on the result.
5. Width, alignment, padding and case
DATA(lv_row) =
|{ lv_material WIDTH = 18 ALIGN = LEFT PAD = '.' } |
&& |{ lv_status CASE = UPPER WIDTH = 10 ALIGN = RIGHT }|.These options are useful for logs and plain-text exports. For UI layouts, let the UI technology handle visual alignment instead of producing space-padded text.
6. Built-in functions inside templates
DATA(lv_name) = ' sap developer '.
DATA(lv_result) =
|Welcome { to_upper( val = condense( val = lv_name ) ) }|.Embedded expressions keep one-time transformations close to the output. If an expression becomes long, compute it before the template so the message remains easy to scan.
7. Multi-line business message
DATA(lv_mail_body) =
|Dear { lv_customer_name },| && cl_abap_char_utilities=>newline &&
|order { lv_vbeln ALPHA = OUT } is ready.| &&
cl_abap_char_utilities=>newline &&
|Total: { lv_amount NUMBER = USER DECIMALS = 2 } { lv_currency }.|.Use an explicit newline value when building mail or log content. For HTML email, construct valid escaped HTML instead of mixing plain-text newlines with markup.
8. Common mistakes
- Do not assume USER formatting is stable across users or interfaces.
- Escape or validate external values before embedding them in HTML, JSON or SQL.
- Avoid expensive method calls repeatedly inside templates used in large loops.
- Keep templates short; pre-calculate complex business rules.
- Check which formatting options exist in your ABAP release and language version.
9. Practical exercise
- Create an order confirmation containing order number, customer, date, amount and currency.
- Display the order without leading zeros.
- Format the amount with two decimal places.
- Create both USER-formatted and stable technical variants, then compare them.
- Move one complex embedded calculation into a helper variable.
10. Interview quick checks
What delimiters define an ABAP string template?
Pipe characters define the template, while braces enclose embedded expressions.
Why can NUMBER = USER be unsuitable for an API payload?
Its result depends on user formatting settings; machine interfaces normally need an explicitly defined stable format.
When should a calculation be moved outside the template?
When it is expensive, repeated or makes the output expression difficult to read and test.
Official SAP references
- String Templates — SAP Keyword Documentation
- Character String and Byte String Processing — SAP Help
- String Template Control Characters Example — SAP Keyword Documentation
Educational examples. Validate formatting behavior, escaping requirements and syntax availability on your ABAP release.
0 Comments