RIGHT
Returns the given number of characters from the end of a string.
MySQL 8.4
RIGHT(str, number_of_characters)- str
- Source stringVARCHAR
- number_of_characters
- Number of characters to extractINT
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.4
SELECT RIGHT('sql-academy', 3);Last digits of a phone number
MySQL 8.4
SELECT name,
RIGHT(phone_number, 4) AS last_digits
FROM Users
LIMIT 5;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.