How to extract a substring from a string? MySQL SUBSTRING Explained
MySQL SUBSTRING() Function is used to return a substring from a string as specified.
Syntax:
SUBSTRING(string, position, length)
or
SUBSTRING(string FROM position FOR length)
If you want to return the whole string from the start position:
SUBSTRING(string FROM position)
or:
SUBSTRING(string, position)
Parameters
Parameter | Description |
---|---|
string | Required. The string to extract from |
position | Required. The start position |
length | Optional. The number of characters to extract. |
Example:
SELECT SUBSTRING("This is a string", 11, 6) AS ExtractString;
The output will be: string
You can use negative value for position
. In this case, the beginning of the string will be the character from the end of the string.
SELECT SUBSTRING("international", -5, 3) AS ExtractString;
The output will be ion
Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS.