Watch the 7.3 Webinar On-Demand
This new release brings updates to Universal Storage, query
optimization, and usability that you won’t want to miss.
Extracts the portion of a string up to the given number of occurrences of a delimiter. This is mainly useful for delimited strings, such as a CSV or ASCII table. If the count
argument is negative, the delimiters will be counted starting from the right, and the portion of the string to the right of the final delimiter will be returned.
SUBSTRING_INDEX(str, delimiter, count)
String
SELECT SUBSTRING_INDEX('a b c d', ' ', 3);
+------------------------------------+
| SUBSTRING_INDEX('a b c d', ' ', 3) |
+------------------------------------+
| a b c |
+------------------------------------+
SELECT SUBSTRING_INDEX('de305d54-75b4-431b-adb2-eb613', '-', 2) as uuid;
+---------------+
| uuid |
+---------------+
| de305d54-75b4 |
+---------------+
SELECT SUBSTRING_INDEX('01-23-45-67-89', '-', -2);
+--------------------------------------------+
| SUBSTRING_INDEX('01-23-45-67-89', '-', -2) |
+--------------------------------------------+
| 67-89 |
+--------------------------------------------+