Function Reference

SUBSTR

A synonym for SUBSTRING: returns a substring from the given position.

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

Three characters from position five

MySQL 8.4
SELECT SUBSTR('sql-academy', 5, 3);
SUBSTR('sql-academy', 5, 3)
aca

Email with a hidden middle

MySQL 8.4
SELECT name,
	CONCAT(SUBSTR(email, 1, 2), '****', SUBSTR(email, -4)) AS masked_email
FROM Users
LIMIT 5;
namemasked_email
Bruce Willisba****.com
George Clooneyte****.com
Kevin Costnerme****.com
Donald Sutherlandra****.net
Jennifer Lopezba****.com

See also

Where to learn