The SUBSTRING_INDEX function is used to extract a portion of a string based on a specified delimiter. By specifying the number of delimiter occurrences, extraction can be performed from either the left or right side.
SUBSTRING_INDEX(<content>, <delimiter>, <field>)
| Parameter | Description |
|---|---|
<content> | The source string to extract from. Type: VARCHAR |
<delimiter> | The delimiter string, case-sensitive and supports multi-byte characters. Type: VARCHAR |
<field> | Number of delimiter occurrences. Positive counts from left, negative counts from right, 0 returns empty string. Type: INT |
Returns VARCHAR type, representing the extracted substring.
Extraction rules:
Special cases:
SELECT SUBSTRING_INDEX('hello world', ' ', 1), SUBSTRING_INDEX('one,two,three', ',', 2);
+----------------------------------------+------------------------------------------+ | SUBSTRING_INDEX('hello world', ' ', 1) | SUBSTRING_INDEX('one,two,three', ',', 2) | +----------------------------------------+------------------------------------------+ | hello | one,two | +----------------------------------------+------------------------------------------+
SELECT SUBSTRING_INDEX('hello world', ' ', -1), SUBSTRING_INDEX('one,two,three', ',', -1);
+-----------------------------------------+-------------------------------------------+ | SUBSTRING_INDEX('hello world', ' ', -1) | SUBSTRING_INDEX('one,two,three', ',', -1) | +-----------------------------------------+-------------------------------------------+ | world | three | +-----------------------------------------+-------------------------------------------+
SELECT SUBSTRING_INDEX(NULL, ',', 1), SUBSTRING_INDEX('test', NULL, 1);
+--------------------------------+------------------------------------+ | SUBSTRING_INDEX(NULL, ',', 1) | SUBSTRING_INDEX('test', NULL, 1) | +--------------------------------+------------------------------------+ | NULL | NULL | +--------------------------------+------------------------------------+
SELECT SUBSTRING_INDEX('hello world', ' ', 0), SUBSTRING_INDEX('a,b,c', ',', 0);
+----------------------------------------+----------------------------------+ | SUBSTRING_INDEX('hello world', ' ', 0) | SUBSTRING_INDEX('a,b,c', ',', 0) | +----------------------------------------+----------------------------------+ | | | +----------------------------------------+----------------------------------+
SELECT SUBSTRING_INDEX('hello world', ',', 1), SUBSTRING_INDEX('no-delimiter', '|', -1);
+----------------------------------------+------------------------------------------+ | SUBSTRING_INDEX('hello world', ',', 1) | SUBSTRING_INDEX('no-delimiter', '|', -1) | +----------------------------------------+------------------------------------------+ | hello world | no-delimiter | +----------------------------------------+------------------------------------------+
SELECT SUBSTRING_INDEX('a,b,c', ',', 5), SUBSTRING_INDEX('a,b,c', ',', -5);
+----------------------------------+-----------------------------------+ | SUBSTRING_INDEX('a,b,c', ',', 5) | SUBSTRING_INDEX('a,b,c', ',', -5) | +----------------------------------+-----------------------------------+ | a,b,c | a,b,c | +----------------------------------+-----------------------------------+
SELECT SUBSTRING_INDEX('ṭṛì→ḍḍumai→hello', '→', 1), SUBSTRING_INDEX('ṭṛì→ḍḍumai→hello', '→', -1);
+-----------------------------------------------+------------------------------------------------+ | SUBSTRING_INDEX('ṭṛì→ḍḍumai→hello', '→', 1) | SUBSTRING_INDEX('ṭṛì→ḍḍumai→hello', '→', -1) | +-----------------------------------------------+------------------------------------------------+ | ṭṛì | hello | +-----------------------------------------------+------------------------------------------------+
SELECT SUBSTRING_INDEX('data::field::value', '::', 2), SUBSTRING_INDEX('data::field::value', '::', -1);
+---------------------------------------------+----------------------------------------------+ | SUBSTRING_INDEX('data::field::value', '::', 2) | SUBSTRING_INDEX('data::field::value', '::', -1) | +---------------------------------------------+----------------------------------------------+ | data::field | value | +---------------------------------------------+----------------------------------------------+
SELECT SUBSTRING_INDEX('', ' ', 1);
+-----------------------------+ | SUBSTRING_INDEX('', ' ', 1) | +-----------------------------+ | | +-----------------------------+
SUBSTRING_INDEX, SUBSTRING