MySQL Substr String

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

SELECT SUBSTR(Str, Position) FROM Source

SELECT SUBSTR(Str FROM Position) FROM Source

SELECT SUBSTR(Str, Position, Length) FROM Source

SELECT SUBSTR(Str FROM Position FOR Length) FROM Source

MySQL Substr function Example

The following query shows multiple ways to use this 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

In this MySQL example, we use the -ve values as the argument position. 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;
Example 2