Pages

Wednesday, March 17, 2021

Split the string in SQL - IBM i

Split String

Substring from a string can be extracted using %SUBST BIF in RPG, %SST in CL and SUBSTRING in SQL by passing from position and the numbers of characters to be extracted. 

But, if we need to split the string based on a specific delimiter and if we don't know the positions? One way to do this is by retrieving the position of a delimiter in the string using %SCAN BIF. This would require more logic to be written if the string needs to be split into multiple substrings. 

This has been made easier with the use of SPLIT table function in SQL. This function is present in SYSTOOLS library.

SPLIT table function accepts three parameters. 
  • INPUT_LIST (Input List) - String(s) that needs to be split.
  • DELIMITER (Delimiter) - String or character that needs to be considered as separator. Both Input List and Delimiter are mandatory parameters. 
  • ESCAPE (Escape) - A character string of length '1' that is to be used as escape character. Delimiter followed by escape character would not be considered as separator. This is an optional parameter.

Let's have a quick look at the simple example to understand this better. 

E.g.:

Split the string into words (consider blank space as delimiter). 

Split string into words in SQL - IBM i

In the above query, we are only passing the two mandatory parameters Input list and Delimiter. 

This table function returns the below two columns. 
  • ORDINAL_POSITION (Ordinal Position) -  Position of the substring in the result. Starts from 1.
  • ELEMENT (Element) - Substring that is split. 
Result of the above query would be as below. 

Split string in SQL - IBM i

Above query has split all the words followed by space and returned as a separate row. 

In case if we need some of the delimiters to be skipped followed by a specific (escape) character, we can pass the optional parameter ESCAPE with the corresponding character. 

Split the string in SQL - IBM i

In the above example, 
  • We are passing back slash (\) as escape character. 
  • Table function would ignore the delimiter followed by the escape character and would not split the the string. 
  • Escape character wouldn't present in the substring split. 
Split string in SQL - IBM i

In both these examples, we are passing the string exclusively which may not always be the case if we need to use this function in the procedures or programs. 

We can use pass the columns from the other tables in query and specify the delimiter. 

Let's have a look at another simple example. 

Split string in SQL - IBM i

In the above query,
  • Line - 1: TESTTABLE is a table with just one character column (CHARFIELD). 
  • Line - 2: Table function SPLIT from SYSTOOLS library. 
  • Line - 3: First parameter INPUT_LIST for SPLIT table function. We are passing one column CHARFIELD from the table TESTTABLE. We are using TRIM so any leading or trailing blanks wouldn't be considered.
  • Line - 4: Second parameter DELIMITER for SPLIT table function. We are passing blanks, so string would be split into words like in previous example.
  • We aren't passing the third and optional parameter ESCAPE. This can be passed if required. 

Let's have a look at the result.

Split string in SQL - IBM i

In the result, 
  • First column CHARFIELD is from the table TESTTABLE, original data in the table before it is split. 
  • Second column ORDINAL_POSITION returns the position of the substring. 
  • Third Column ELEMENT returns the substring. 
There would be multiple rows of substrings for each row in the original table. 

*This function is only available since IBM i 7.3 TR6 and higher. 

 
If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form. 

No comments:

Post a Comment

Popular Posts