Unstring Statement Example


Scenario - Split the string using UNSTRING statement.

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. UNSTREXP.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-INPUT       PIC X(70) VALUE
             'Mainframestechhelp, is a mainframe Community'.
          05 WS-OUTPUT1     PIC X(30). 
          05 WS-OUTPUT2     PIC X(40).

       PROCEDURE DIVISION.
           UNSTRING WS-INPUT DELIMITED BY ","
               INTO WS-OUTPUT1, WS-OUTPUT2
                    ON OVERFLOW DISPLAY "ERROR OCCURED"
                NOT ON OVERFLOW
                        DISPLAY "WS-OUTPUT1:  ", WS-OUTPUT1 
                        DISPLAY "WS-OUTPUT2:  ", WS-OUTPUT2
           END-UNSTRING.

           STOP RUN.

Output -

WS-OUTPUT1:  Mainframestechhelp
WS-OUTPUT2:  is a Mainframe Community

Explaining Example -

In the above example:

  • WS-INPUT contains a string that has comma(,) in it.
  • UNSTRING statement is used to seperate the string into two parts by using comma as delimiter.
  • Note that the no of output variables is divided by the no of occurrences of delimiter.