SPLIT Function

Splits a string by a caller-specified separator and either returns the substrings as an array, or returns one specific substring.

Syntax

SPLIT(text, separator, [which-substring])

Parameters

  • text: text
    The string to split up.
  • separator: text
    The string that separates each substring in text.
  • which-substring: integer (optional, non-negative)
    The index of the substring to return. A which-substring value of 0 will return the first substring, 1 will return the second substring, and so on. If no such substring exist in the original string, then NULL is returned.

Return Value

If which-substring is provided, then the specified substring is returned. If it is not provided, then an array of all the substrings is returned.

Example

PRINT SPLIT('AAA|BBB|CCC', '|', 1);  -- "BBB"
PRINT SPLIT('AAA|BBB|CCC', '|', 5); -- NULL
PRINT SPLIT('AAA|BBB|CCC', '|'); -- "[AAA, BBB, CCC]"