MySQL Substr String

The MySQL String 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 the end to the start position.

The basic syntax of the MySQL Substr string is as shown below.

SELECT SUBSTR(Str, Position) FROM Source


SELECT SUBSTR(Str, Position, Length) FROM Source

SELECT SUBSTR(Str FROM Position FOR Length) FROM Source

MySQL String 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;
Substr Example 2

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.