Function Reference

SUBSTRING

Returns a substring that starts at the given position.

MySQL 8.4
SUBSTRING(str, pos[, len])
  • [ ]optional part
str
Source string
VARCHAR
pos
Start position, counting from 1; a negative one counts from the end of the string
INT
len
Length of the substring, up to the end of the string by default
INTOptional

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.4
SELECT SUBSTRING('sql-academy', 5);
SUBSTRING('sql-academy', 5)
academy

Substring of a given length

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

Position counted from the end

MySQL 8.4
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