The SPLIT statement is used to divide a character-like variable (e.g., a string or a field of type C) into multiple substrings (segments) based on a defined delimiter. The result of this split can be written either into individual variables or – which is often more practical – into the rows of an internal table.
Syntax
SPLIT <source> AT <delimiter> INTO { {<target1> <target2> ...} | {TABLE <target_table>} } [IN CHARACTER MODE | IN BYTE MODE].SPLIT <source>: Specifies the character-like variable (or literal) to be split.AT <delimiter>: (Mandatory) Specifies the character or string (literal or variable) that serves as the delimiter. The source is split at each occurrence of this delimiter.INTO <target1> <target2> ...: One way to receive the results. The found segments are written sequentially into the variables<target1>,<target2>, etc. These should also be character-like (oftenSTRINGorC).- Behavior: If more segments are found than target variables available, the entire remainder of the source (including remaining delimiters!) is written into the last specified target variable. If fewer segments are found, the excess target variables are reset to their initial value (
CLEAR).
- Behavior: If more segments are found than target variables available, the entire remainder of the source (including remaining delimiters!) is written into the last specified target variable. If fewer segments are found, the excess target variables are reset to their initial value (
INTO TABLE <target_table>: The more flexible method. Each found segment is written as a new row into the internal table<target_table>. The table should have a suitable character-like row type (e.g.,STANDARD TABLE OF string). Important: The target table is automatically cleared (CLEAR) before theSPLIT!IN CHARACTER MODE: (Default for character-like source/delimiter) Processes the data character by character.IN BYTE MODE: Processes the data byte by byte (necessary for typeX,XSTRING).
How It Works
- The
<source>is searched from left to right for the<delimiter>. - The strings between the found delimiters form the result segments.
- The delimiters themselves are removed and are not part of the segments.
- If delimiters directly follow each other or are at the beginning/end of the source, empty segments are created.
- The segments are written either into the target variables or as rows into the target table.
System Field sy-subrc
sy-subrc = 0: Is set in most cases, even if the delimiter was not found at all (then the entire source ends up in the first target) or if empty segments were created.sy-subrc = 4: Can occur in rare cases, e.g., whenINTO <target1>is used, the delimiter was not found, and the source is longer than<target1>can hold.- Recommendation: Don’t rely too much on
sy-subrc. Instead, directly check the results (e.g.,lines( target_table )or the content of the target variables).
Examples
1. Split Comma-Separated String into Variables
DATA lv_csv TYPE string VALUE 'Miller,Petra,Hamburg'.DATA: lv_lastname TYPE string, lv_firstname TYPE string, lv_city TYPE string.
SPLIT lv_csv AT ',' INTO lv_lastname lv_firstname lv_city.
WRITE: / 'Last name:', lv_lastname, " -> Miller / 'First name:', lv_firstname, " -> Petra / 'City:', lv_city. " -> Hamburg2. Split a Sentence into Words into a Table
DATA lv_sentence TYPE string VALUE 'An example with spaces'. " Multiple spacesDATA lt_words TYPE STANDARD TABLE OF string.
SPLIT lv_sentence AT space INTO TABLE lt_words. " space is constant for ' '
LOOP AT lt_words INTO DATA(lv_word). WRITE / 'Segment:', |'{ lv_word }'|. " Note the empty segments!ENDLOOP.Output:
Segment: 'An'Segment: ''Segment: ''Segment: 'example'Segment: ''Segment: 'with'Segment: ''Segment: 'spaces'3. Handling More Segments Than Target Variables
DATA lv_parts TYPE string VALUE 'Part1-Part2-Part3-Part4'.DATA: lv_p1 TYPE string, lv_p2 TYPE string. " Only 2 target variables
SPLIT lv_parts AT '-' INTO lv_p1 lv_p2.
WRITE: / 'P1:', lv_p1, " -> Part1 / 'P2:', lv_p2. " -> Part2-Part3-Part4 (remainder ends up here!)4. Demonstrating Empty Segments
DATA lv_data TYPE string VALUE '|ValueA||ValueC|'. " Missing value B, leading/trailing delimiterDATA lt_segments TYPE STANDARD TABLE OF string.
SPLIT lv_data AT '|' INTO TABLE lt_segments.
LOOP AT lt_segments INTO DATA(lv_segment). WRITE / sy-tabix, '. Segment:', |'{ lv_segment }'|.ENDLOOP.Output:
1 . Segment: '' 2 . Segment: 'ValueA' 3 . Segment: '' 4 . Segment: 'ValueC' 5 . Segment: ''Best Practices
SPLITdoes not modify the original source variable.- The delimiters are lost during the split.
- Be aware that empty segments can be created.
- Using
INTO TABLEis generally more flexible and robust, as you don’t have to worry about the exact number of segments. The table is always automatically initialized (cleared) before use. SPLITis very useful for parsing simple structured data like CSV lines or path specifications.