The TRANSLATE statement in ABAP is used to manipulate the content of a character-like data object (such as STRING, C, N, D, T) character by character. It has two main functions:
- Case conversion: Converts the entire text to uppercase or lowercase.
- Character replacement: Replaces certain characters with other characters according to a defined replacement rule (
USINGaddition).
It’s important that TRANSLATE directly modifies the specified variable (the change happens “in place”).
Syntax
There are two basic forms:
-
Change case:
TRANSLATE <variable> TO UPPER CASE.TRANSLATE <variable> TO LOWER CASE. -
Replace characters using a rule:
TRANSLATE <variable> USING <rule>.
Components:
<variable>: The character-like variable whose content should be modified.TO UPPER CASE: Converts all lowercase letters (including language-specific ones like ä, ö, ü, ß) in the<variable>to the corresponding uppercase letters (Ä, Ö, Ü, SS). Digits and special characters remain unchanged.TO LOWER CASE: Converts all uppercase letters (including Ä, Ö, Ü) in the<variable>to the corresponding lowercase letters (ä, ö, ü). Digits and special characters remain unchanged.USING <rule>: Performs a character-by-character replacement.<rule>is a character-like data object (literal or variable) containing the replacement rules in pairs:- The rule is interpreted as
XYAB...: Replace characterXwithY, replace characterAwithB, and so on. - The length of
<rule>must be even. - The
<variable>is traversed character by character. For each character, it checks whether it appears at an odd position (1, 3, 5, …) in the<rule>. - If the character is found at the first matching odd position
2n-1in<rule>, it is replaced in<variable>by the character at the even position2nin<rule>. - If a character from
<variable>doesn’t appear at an odd position in<rule>, it remains unchanged.
- The rule is interpreted as
Modern Alternatives (Preferred!)
Although TRANSLATE is functional, modern ABAP often offers preferred alternatives that are more flexible and return a new value instead of modifying the original variable:
- Case conversion: The built-in functions
to_upper( text )andto_lower( text ). - Character replacement: The built-in function
replace( val = ... sub = ... with = ... ). This function is more powerful as it can also replace entire strings (substrings) and not just individual characters, and also offers options for regular expressions.
Examples
1. Convert to Uppercase
DATA text_mixed TYPE string VALUE 'A test with Upper and lower case.'.WRITE: / 'Original:', text_mixed.
TRANSLATE text_mixed TO UPPER CASE.WRITE: / 'Upper Case:', text_mixed.
" Modern alternative:DATA(upper_result) = to_upper( 'A test with Upper and lower case.' ).WRITE: / 'Upper (Function):', upper_result.Output:
Original: A test with Upper and lower case.Upper Case: A TEST WITH UPPER AND LOWER CASE.Upper (Function): A TEST WITH UPPER AND LOWER CASE.2. Convert to Lowercase
DATA text_caps TYPE c LENGTH 30 VALUE 'ONLY UPPERCASE'.WRITE: / 'Original:', text_caps.
TRANSLATE text_caps TO LOWER CASE.WRITE: / 'Lower Case:', text_caps.
" Modern alternative:DATA(lower_result) = to_lower( 'ONLY UPPERCASE' ).WRITE: / 'Lower (Function):', lower_result.Output:
Original: ONLY UPPERCASELower Case: only uppercaseLower (Function): only uppercase3. Replace Characters with USING (Remove Umlauts)
DATA umlaut_text TYPE string VALUE 'Schöne Grüße aus Österreich'.DATA rule TYPE c LENGTH 12 VALUE 'ÄAÖOÜUäaöoüu'. " Ä->A, Ö->O, Ü->U, ä->a, ...
WRITE: / 'Original:', umlaut_text.TRANSLATE umlaut_text USING rule.WRITE: / 'Without umlauts:', umlaut_text.
" Modern alternative (example for ä):DATA(no_ae) = replace( val = 'Schöne Grüße' sub = 'ä' with = 'ae' occ = 0 ).WRITE: / 'With replace (ae):', no_ae.Output:
Original: Schöne Grüße aus ÖsterreichWithout umlauts: Schone Gruße aus OsterreichWith replace (ae): Schoene Gruesse(You can see that replace is more flexible and can also replace with multiple characters).
4. Replace Characters with USING (Character Mapping)
DATA code TYPE c LENGTH 5 VALUE 'A1B2C'.DATA map_rule TYPE c LENGTH 10 VALUE 'ABCDE12345'. " A->1, B->2, C->3, D->4, E->5
WRITE: / 'Code Original:', code.TRANSLATE code USING map_rule.WRITE: / 'Code Mapped:', code.Output:
Code Original: A1B2CCode Mapped: 11223(A becomes 1, 1 is not found in the rule (only at even position), B becomes 2, 2 not found, C becomes 3).
Best Practices
TRANSLATEmodifies the variable directly. If the original value is needed, it must be saved beforehand.TRANSLATE ... USINGis a pure character-to-character replacement. It cannot search for strings or add/delete characters (only replace with others).- For more complex replacements (whole words, patterns via regex) or when the original value should be preserved, the modern functions
to_upper(),to_lower(), and especiallyreplace()are usually the better choice. - However,
TRANSLATEcan still be useful for simple, performant case conversions or multiple 1-to-1 character replacements.