SQL Notebook (logo)

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])

Arguments

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.

Examples

  1. DECLARE @data = SPLIT('AAA|BBB|CCC', '|', 1);
    Assigns "BBB" to the variable @data.
  2. DECLARE @data = SPLIT('AAA|BBB|CCC', '|', 5);
    Assigns NULL to the variable @data.
  3. DECLARE @data = ARRAY_COUNT(SPLIT('AAA|BBB|CCC', '|'));
    Assigns 3 to the variable @data.