MySQL: SUBSTRING_INDEX Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL SUBSTRING_INDEX Function

In this guide, we will explain how to use the MySQL SUBSTRING_INDEX function with syntax and examples.

Description

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

Syntax

The syntax for the SUBSTRING_INDEX function in MySQL is:

SUBSTRING_INDEX( string, delimiter, number )

Parameters or Arguments

string

The source string.

delimiter

The delimiter to search for in string.numberThe number of times to search for delimiter.

Note

  • If number is a positive value, everything from the left of the targeted delimiter is returned by the SUBSTRING_INDEX function.
  • If number is a negative value, everything from the right of the targeted delimiter is returned by the SUBSTRING_INDEX function.
  • See also SUBSTRING function.

Applies To

The SUBSTRING_INDEX function can be used in the following versions :

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let’s look at some SUBSTRING_INDEX function examples and explore how to use the SUBSTRING_INDEX function.

For example:

mysql> SELECT SUBSTRING_INDEX('www.adglob.in', '.', 1);
Result: 'www'

mysql> SELECT SUBSTRING_INDEX('www.adglob.in', '.', 2);
Result: 'www.adglob'

mysql> SELECT SUBSTRING_INDEX('www.adglob.in', '.', -1);
Result: 'in'

mysql> SELECT SUBSTRING_INDEX('www.adglob.in', '.', -2);
Result: 'adglob.in'

Next Topic : Click Here

This Post Has One Comment

Leave a Reply