Mariadb Substr Function

MariaDB: SUBSTR Function

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

Description

The MariaDB SUBSTR function allows you to extract a substring from a string.

Syntax

The syntax for the SUBSTR function in MariaDB is:

SUBSTR( string, start_position, [ length ] )

OR

SUBSTR( string FROM start_position [ FOR length ] )

Parameters or Arguments

string

The source string.

start_position

The position for extraction. The first position in the string is always 1.

length

Optional. It is the number of characters to extract. If this parameter is omitted, it will return the entire string.

Note

  • The first position in string is 1.
  • If start_position is a positive number, then the SUBSTR function starts from the beginning of the string.
  • If start_position is a negative number, then the SUBSTR function starts from the end of the string and counts backwards.
  • The SUBSTR function and the MID function are synonyms of the SUBSTRING function.

Applies To

The SUBSTR function can be used in the following versions of MariaDB:

  • MariaDB 10

Example

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

For example:

SELECT SUBSTR('AODBA.com', 5);
Output: 'OnTheNet.com'

SELECT SUBSTR('AODBA.com' FROM 5);
Output: 'OnTheNet.com'

SELECT SUBSTR('AODBA.com', 5, 2);
Output: 'On'

SELECT SUBSTR('AODBA.com' FROM 5 FOR 2);
Output: 'Tech'

SELECT SUBSTR('AODBA.com', -7, 3);
Output: 'Net'

SELECT SUBSTR('AODBA.com' FROM -7 FOR 3);
Output: 'Net'