跳到主要内容

SUBSTRING_INDEX

Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned.

Syntax

SUBSTRING_INDEX(str,delim,count);

Arguments

ArgumentsDescription
strThe main string from where the character to be extracted
delimThe delimiter
countThe number of occurrences

Return Type

String data type value.

Examples

SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
+------------------------------------------+
| SUBSTRING_INDEX('www.mysql.com', '.', 2) |
+------------------------------------------+
| www.mysql |
+------------------------------------------+

SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
+----------------------------------------------+
| SUBSTRING_INDEX('www.mysql.com', '.', (- 2)) |
+----------------------------------------------+
| mysql.com |
+----------------------------------------------+