The SPLIT_PART function splits a string into multiple parts according to the specified separator and return one of the parts.
SPLIT_PART ( <str>, <separator>, <part_index> )
| Parameter | Description |
|---|---|
<str> | The string to be split |
<separator> | The string used for splitting |
<part_index> | The index of the part to be returned. Starting from 1 |
Returns the specified part of the string split according to the delimiter. Special cases:
<part_index> is 0, NULL is returned.SELECT SPLIT_PART('hello world', ' ', 1);
+----------------------------------+ | SPLIT_PART('hello world', ' ', 1) | +----------------------------------+ | hello | +----------------------------------+
SELECT SPLIT_PART('apple,banana,cherry', ',', 2);
+-------------------------------------------+ | SPLIT_PART('apple,banana,cherry', ',', 2) | +-------------------------------------------+ | banana | +-------------------------------------------+
SELECT SPLIT_PART('apple,banana,cherry', ',', 0);
+-------------------------------------------+ | SPLIT_PART('apple,banana,cherry', ',', 0) | +-------------------------------------------+ | NULL | +-------------------------------------------+
SELECT SPLIT_PART('apple,banana,cherry', ',', -1), SPLIT_PART('apple,banana,cherry', ',', -2);
+--------------------------------------------+--------------------------------------------+ | SPLIT_PART('apple,banana,cherry', ',', -1) | SPLIT_PART('apple,banana,cherry', ',', -2) | +--------------------------------------------+--------------------------------------------+ | cherry | banana | +--------------------------------------------+--------------------------------------------+
SELECT SPLIT_PART('apple,banana', ',', 5), SPLIT_PART('apple,banana', ',', -5);
+-----------------------------------+------------------------------------+ | SPLIT_PART('apple,banana', ',', 5) | SPLIT_PART('apple,banana', ',', -5) | +-----------------------------------+------------------------------------+ | | | +-----------------------------------+------------------------------------+
SELECT SPLIT_PART(NULL, ',', 1), SPLIT_PART('test', NULL, 1), SPLIT_PART('test', ',', NULL);
+---------------------------+-----------------------------+-------------------------------+ | SPLIT_PART(NULL, ',', 1) | SPLIT_PART('test', NULL, 1) | SPLIT_PART('test', ',', NULL) | +---------------------------+-----------------------------+-------------------------------+ | NULL | NULL | NULL | +---------------------------+-----------------------------+-------------------------------+
SELECT SPLIT_PART('', ',', 1), SPLIT_PART('test', '', 2);
+------------------------+---------------------------+ | SPLIT_PART('', ',', 1) | SPLIT_PART('test', '', 2) | +------------------------+---------------------------+ | NULL | | +------------------------+---------------------------+
SELECT SPLIT_PART('hello world', '|', 1), SPLIT_PART('hello world', '|', 2);
+-----------------------------------+-----------------------------------+ | SPLIT_PART('hello world', '|', 1) | SPLIT_PART('hello world', '|', 2) | +-----------------------------------+-----------------------------------+ | NULL | NULL | +-----------------------------------+-----------------------------------+
SELECT SPLIT_PART('a,,c', ',', 1), SPLIT_PART('a,,c', ',', 2), SPLIT_PART('a,,c', ',', 3);
+----------------------------+----------------------------+----------------------------+ | SPLIT_PART('a,,c', ',', 1) | SPLIT_PART('a,,c', ',', 2) | SPLIT_PART('a,,c', ',', 3) | +----------------------------+----------------------------+----------------------------+ | a | | c | +----------------------------+----------------------------+----------------------------+
SELECT SPLIT_PART('ṭṛì ḍḍumai ṭṛì', ' ', 2);
+--------------------------------------+ | SPLIT_PART('ṭṛì ḍḍumai ṭṛì', ' ', 2) | +--------------------------------------+ | ḍḍumai | +--------------------------------------+
SPLIT_PART, SPLIT