RIGHT() in MySQL — How It Works
What the MySQL RIGHT() function does, with an example.
Published October 18, 2026
RIGHT(str, n) returns the last n characters of a string.
How it works
If n exceeds the length the whole string is returned.
Try it in MySQL
SELECT RIGHT('abcdef', 2)
ef
SELECT RIGHT('2026-11-12', 2)
12
SELECT RIGHT('ab', 5)
ab
Practical examples
Example 1
SELECT RIGHT('abcdef', 2);
-- efExample 2
SELECT RIGHT('2026-11-12', 2);
-- 12Example 3
SELECT RIGHT('ab', 5);
-- abUse it in a real query
SELECT id, RIGHT('abcdef', 2) AS result
FROM your_table
LIMIT 10;Common mistakes
- Negative lengths returning empty strings.
- Using it on dates instead of DATE functions.
- Forgetting trailing spaces count.
Key takeaways
- RIGHT() in one line: RIGHT(str, n) returns the last n characters of a string.
- Try queries in a test database first.
- Check NULL handling and data types.
FAQ
Get file extension?
SUBSTRING_INDEX(filename, '.', -1).
Does RIGHT() work in other databases?
Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.