Function Reference

SUBSTRING

Returns a substring that starts at the given position.

MySQL 8.1
SUBSTRING(str, pos[, len])

Parameters

strVARCHAR
Source string
posINT
Start position of the substring, a negative one counts from the end of the string
lenINToptional
Length of the substring, up to the end of the string by default

Return value

A substring; an empty string if pos is 0 or greater than the string length, or len is less than 1

Examples

Rest of the string from a position

MySQL 8.1
SELECT SUBSTRING('sql-academy', 5);
SUBSTRING('sql-academy', 5)
academy

Substring of a given length

MySQL 8.1
SELECT SUBSTRING('sql-academy', 1, 3);
SUBSTRING('sql-academy', 1, 3)
sql

Position counted from the end

MySQL 8.1
SELECT SUBSTRING('sql-academy', -3);
SUBSTRING('sql-academy', -3)
emy

Details

The same function is available as SUBSTR and in the standard form SUBSTRING(str FROM pos FOR len).

See also

Where to learn