- Categories:
String & binary functions (General) , Table functions
STRTOK_ SPLIT_ TO_ TABLE¶
Tokenizes a string with the given set of delimiters and flattens the results into rows.
- See also:
Syntax¶
Arguments¶
Required:
stringText to be tokenized.
Optional:
delimiter_listOptional set of delimiters. The default value is a single space character.
Output¶
This function returns the following columns:
| Column name | Data type | Description |
|---|---|---|
| SEQ | NUMBER | A unique sequence number associated with the input record. The sequence is not guaranteed to be gap-free or ordered. in any particular way. |
| INDEX | NUMBER | The one-based index of the element. |
| VALUE | VARCHAR | The value of the element of the flattened array. |
Note
The query can also access the columns of the original (correlated) table that served as the source of data for this function. If a single row from the original table resulted in multiple rows in the flattened view, the values in this input row are replicated to match the number of rows produced by this function.
Usage notes¶
- If string is an empty string, NULL, or contains only delimiter characters, the function returns no rows. This behavior differs from SPLIT_TO_TABLE, which returns one row for an empty string.
- Because the function returns no rows for these inputs, the corresponding input rows are omitted from the
result of a lateral join, even when you specify
LEFT JOIN LATERAL. To keep every input row, use STRTOK_TO_ARRAY with FLATTEN and specifyOUTER => TRUE. For an example, see Keep input rows that produce no tokens.
Examples¶
Here is a simple example on constant input.
Create a table and insert data:
You can use the LATERAL keyword with the STRTOK_SPLIT_TO_TABLE function
so that the function executes on each row of the splittable_strtok table as a correlated table:
This example is the same as the preceding, except that it specifies multiple delimiters:
Create another table that contains authors in one column and some of their book titles in another column. In the table data, the book titles might be separated by a comma or a semi-colon:
Use the LATERAL keyword and the SPLIT_TO_TABLE function to run a query that returns a separate row for each title.
In addition, use the TRIM function to remove leading and trailing spaces from the titles. Note that the SELECT
list includes the fixed value column that is returned by the function:
Keep input rows that produce no tokens¶
STRTOK_SPLIT_TO_TABLE returns no rows when the input string is empty or NULL, so a lateral join omits those
input rows, even with LEFT JOIN LATERAL. Create a table that contains an empty string and a NULL value:
The following query returns rows only for id 2:
To keep every input row, use STRTOK_TO_ARRAY with FLATTEN and specify OUTER => TRUE: