MySQL Substr String

The MySQL Substr function is a synonym of a Substring function that returns a substring. The Substr string in MySQL also allows you to place negative values as the Position argument. If you use negative values, then substr start looking from end to start position. The basic syntax of the Substr string in MySQL is as shown below.

SELECT SUBSTR(String, Position) FROM Source

SELECT SUBSTR(String FROM Position) FROM Source

SELECT SUBSTR(String, Position, Length) FROM Source

SELECT SUBSTR(String FROM Position FOR Length) FROM Source

MySQL Substr function Example 1

The following query shows multiple ways to use this Substr function.

SELECT SUBSTR('MySQL Tutorial', 7) AS Substr1;

SELECT SUBSTR('MySQL Tutorial' FROM 9) AS Substr2;

SELECT SUBSTR('MySQL Tutorial', 3, 14) AS Substr3;

SELECT SUBSTR('MySQL Tutorial' FROM 3 FOR 11) AS Substr4;
MySQL Substr String 1

Substr String Example 2

In this MySQL example, we use the -ve values as the argument position in a substr function. I suggest you refer to the Substring function article to understand this substr function.

SELECT SUBSTRING('Tutorial Gateway', -7) AS Substr1;

SELECT SUBSTRING('Tutorial Gateway', -14, 8) AS Substr2;
MySQL Substr String 2