Mysql Substr Function

MySQL: SUBSTR Function

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


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


The syntax for the SUBSTR function in MySQL is:

SUBSTR( string, start_position, [ length ] )


SUBSTR( string FROM start_position [ FOR length ] )

Parameters or Arguments


The source string.


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


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


  • 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. Negative values for start_position was introduced in MySQL 4.1.
  • 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 MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1.1


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

For example:

mysql> SELECT SUBSTR('' FROM 5);
Output: ''

mysql> SELECT SUBSTR('', 1, 4);
Output: 'AODB'

mysql> SELECT SUBSTR('' FROM 1 FOR 4);
Output: 'AODB'

mysql> SELECT SUBSTR('', -3, 3);
Output: 'com'

mysql> SELECT SUBSTR('' FROM -3 FOR 3);
Output: 'com'