SUBSTR() FUNCTION in MySQL

SUBSTR() FUNCTION
The MySQL SUBSTR function is used to extract a substring from a string. The various versions of MySQL support the SUBSTR function, namely, MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0 and MySQL 4.1.1.

Syntax 1:

SUBSTR( string, start_position, length )

Syntax 2:

SUBSTR( 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 that is used to specify the number of characters to extract.

Example 1:

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

Output:

‘WORLD’

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

Example 2:

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

Output:

‘WORLD’

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

Example 3:

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

Output:

‘WORLD’

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

Example 4:

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

Output:

‘WORLD’

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