split_part
This function splits a given string according to the separators and returns the requested part. (start counting from the beginning)
Syntaxβ
VARCHAR split_part(VARCHAR content, VARCHAR delimiter, INT field)
Parametersβ
content
: The string to be split. Data type: VARCHAR.
delimiter
: The separator used to split the string. Data type: VARCHAR.
field
: The position of the part to return. Positive values count from the beginning, negative values count from the end. Data type: INT.
Return Valueβ
Returns the specified part of the split string. Data type: VARCHAR.
Examplesβ
MySQL > select split_part("hello world", " ", 1);
+----------------------------------+
|split_part('hello world', ' ', 1) |
+----------------------------------+
| hello |
+----------------------------------+
MySQL > select split_part("hello world", " ", 2);
+-----------------------------------+
| split_part('hello world', ' ', 2) |
+-----------------------------------+
| world |
+-----------------------------------+
MySQL > select split_part("hello world", " ", -1);
+----------------------------------+
|split_part('hello world', ' ', -1) |
+----------------------------------+
| world |
+----------------------------------+
MySQL > select split_part("hello world", " ", -2);
+-----------------------------------+
| split_part('hello world', ' ', -2) |
+-----------------------------------+
| hello |
+-----------------------------------+
MySQL > select split_part("hello world", "|", 1);
+----------------------------------+
| split_part('hello world', '|', 1) |
+----------------------------------+
| hello world |
+----------------------------------+
MySQL > select split_part("hello world", "|", -1);
+-----------------------------------+
| split_part('hello world', '|', -1) |
+-----------------------------------+
| hello world |
+-----------------------------------+
MySQL > select split_part("hello world", "|", 2);
+----------------------------------+
| split_part('hello world', '|', 2) |
+----------------------------------+
| |
+----------------------------------+
MySQL > select split_part("abca", "a", 1);
+----------------------------+
| split_part('abca', 'a', 1) |
+----------------------------+
| |
+----------------------------+
MySQL > select split_part("abca", "a", -1);
+-----------------------------+
| split_part('abca', 'a', -1) |
+-----------------------------+
| |
+-----------------------------+
MySQL > select split_part("abca", "a", -2);
+-----------------------------+
| split_part('abca', 'a', -2) |
+-----------------------------+
| bc |
+-----------------------------+
MySQL > select split_part("2019εΉ΄7ζ8ζ₯", "ζ", 1);
+-----------------------------------------+
| split_part('2019εΉ΄7ζ8ζ₯', 'ζ', 1) |
+-----------------------------------------+
| 2019εΉ΄7 |
+-----------------------------------------+
keywordβ
SPLIT_PART,SPLIT,PART