THE WORLD'S LARGEST WEB DEVELOPER SITE

MySQL SUBSTRING_INDEX() Function

❮ MySQL Functions

Example

Return a substring of a string before a specified number of delimiter occurs:

SELECT SUBSTRING_INDEX("www.w3schools.com", ".", 1);
Try it Yourself »

Definition and Usage

The SUBSTRING_INDEX() function returns a substring of a string before a specified number of delimiter occurs.

Syntax

SUBSTRING_INDEX(string, delimiter, number)

Parameter Values

Parameter Description
string Required. The original string
delimiter Required. The delimiter to search for
number Required. The number of times to search for the delimiter

Note

  • If number is negative, this function returns all to the right of the targeted delimiter
  • If number is positive, this function returns all to the left of the targeted delimiter

Technical Details

Works in: MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

More Examples

Example

Return a substring of a string before a specified number of delimiter occurs:

SELECT SUBSTRING_INDEX("www.w3schools.com", ".", 2);
Try it Yourself »

❮ MySQL Functions