ESQL for splitting a string into mulitple values

ranaa picture ranaa · Jul 27, 2017 · Viewed 8.6k times · Source

Following Input xml field needs to be substringed for 6 characters and each 6 characters should be splitted and saved in the option field of output.

Input: 
<feature>124414500045563879</feature>

output:
<option>124414</option>
<option>500045</option>
<option>563879</option>

Is there any tokenizer function available in IIB ESQL to achieve the above result.

Answer

VincentS picture VincentS · Jul 27, 2017

As far as I know, no there isn't a String Tokenizer function in ESQL.

But you could use the following procedure as base to achieve your goal. This method splits S on Delim into an array in Env (Environment.Split.Array[]) and removes Environment.Split before refilling it.

In your case, you don't need the Delim, you should work with a fixed length in the Substring part.

CREATE PROCEDURE Split (IN S CHARACTER, IN Env REFERENCE, IN Delim CHARACTER) 
BEGIN 
   DECLARE P INTEGER; 
   DECLARE Idx INTEGER 1; 

   SET Env.Split = NULL; 

   REPEAT 
      SET P = POSITION(Delim IN S); 
      IF P = 0 THEN 
         SET Env.Split.Array[Idx] = S; 
      ELSE 
         SET Env.Split.Array[Idx] = LEFT(S, P - 1); 
         SET S = SUBSTRING(S FROM P + LENGTH(Delim)); 
         SET Idx = Idx + 1; 
      END IF; 
  UNTIL P = 0    
  END REPEAT;    
END;

Source: http://www.mqseries.net/phpBB2/viewtopic.php?p=97845&