Function Reference

RIGHT

Returns the given number of characters from the end of a string.

MySQL 8.1
RIGHT(str, number_of_characters)

Parameters

strVARCHAR
Source string
number_of_charactersINT
Number of characters to extract

Return value

The last number_of_characters characters; the whole string if it is shorter; an empty string if number_of_characters is less than 1

Examples

Last three characters

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

Last digits of a phone number

MySQL 8.1
SELECT name,
	RIGHT(phone_number, 4) AS last_digits
FROM Users
LIMIT 5;
namelast_digits
Bruce Willis0128
George Clooney0145
Kevin Costner0197
Donald Sutherland0161
Jennifer Lopez0145

Details

Characters are counted, not bytes. A negative number gives an empty string, while in PostgreSQL it gives the whole string without the first characters: RIGHT('abcdef', -2) returns cdef there.

See also