Skip to main content

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. 

Comments

  1. How can you deal with the situation where there are more than 1 space between words? Must you add WHERE element <> ''?

    ReplyDelete
    Replies
    1. Hi Glenn,

      I suppose, yes. If there are more than 1 space, the function would return a blank entry which can be suppressed by using element <> ' ' as you mentioned.

      Delete

Post a Comment

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i