ID EN
String Functions and Operators

SUBSTRING_INDEX(str,delim,count)

MySQL 8.4

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. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

Syntax

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

Examples

Example

This function is multibyte safe.

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