totn MariaDB Functions

MariaDB: SUBSTRING_INDEX Function

This MariaDB tutorial explains how to use the MariaDB SUBSTRING_INDEX function with syntax and examples.

Description

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

Syntax

The syntax for the SUBSTRING_INDEX function in MariaDB is:

SUBSTRING_INDEX( string, delimiter, number )

Parameters or Arguments

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

Note

  • If number is a negative value, everything from the left of the targeted delimiter is returned by the SUBSTRING_INDEX function.
  • If number is a positive 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 of MariaDB:

  • MariaDB 10

Example

Let's look at some MariaDB SUBSTRING_INDEX function examples and explore how to use the SUBSTRING_INDEX function in MariaDB.

For example:

SELECT SUBSTRING_INDEX('www.checkyourmath.com', '.', 1);
Result: 'www'

SELECT SUBSTRING_INDEX('www.checkyourmath.com', '.', 2);
Result: 'www.checkyourmath'

SELECT SUBSTRING_INDEX('www.checkyourmath.com', '.', -1);
Result: 'com'

SELECT SUBSTRING_INDEX('www.checkyourmath.com', '.', -2);
Result: 'checkyourmath.com'