MySQL SUBSTRING_INDEX() Function
THE WORLD'S LARGEST WEB DEVELOPER SITE

MySQL SUBSTRING_INDEX() Function

❮ MySQL Functions

Example

Return the substring of string before number of occurrences of delimiter:

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

Definition and Usage

The SUBSTRING_INDEX() function returns the substring of string before number of occurrences of delimiter.

Syntax

SUBSTRING_INDEX(string, delimiter, number)

Parameter Values

Parameter Description
string Required. The source string
delimiter Required. The delimiter to search for in string
number Required. The number of times to search for delimiter

Note

  • If number is negative, the SUBSTRING_INDEX() function returns everything to the right of the targeted delimiter
  • If number is positive, the SUBSTRING_INDEX() function returns everything 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 the substring of string before number of occurrences of delimiter:

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

❮ MySQL Functions