SUBSTRING() FUNCTION in MySQL

SUBSTRING() FUNCTION
The MySQL SUBSTRING function is used to extract a substring from a string. The various versions of MySQL supports the SUBSTRING function, namely, MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0 and MySQL 3.23.

Syntax 1:

SUBSTRING( string, start_position, length )

Syntax 2:

SUBSTRING( string FROM start_position FOR length )

Parameters:
string: It is used to specify the string to search.
start_position: It is used to specify the position to begin extraction.
length: It is an optional parameter which is used to specify the number of characters to extract.

Example 1:

mysql> SELECT SUBSTRING ( 'HELLO WORLD', 7 );

Output:

‘WORLD’

Explanation:
The substring is extracted from the 7th position till the end.

Example 2:

mysql> SELECT SUBSTRING ( 'HELLO WORLD', 7, 5 );

Output:

‘WORLD’

Explanation:
The substring is extracted from the 7th position for 5 characters.

Example 3:

mysql> SELECT SUBSTRING ( 'HELLO WORLD' FROM 7 );

Output:

‘WORLD’

Explanation:
The substring is extracted from the 7th position till the end.

Example 4:

mysql> SELECT SUBSTRING ( 'HELLO WORLD' FROM 7 FOR 5 );

Output:

‘WORLD’

Explanation:
The substring is extracted from the 7th position for 5 characters.

Please Share