Function Reference

RIGHT

Returns the last n characters of a string.

PostgreSQL 17.9
RIGHT(string, n)
string
Source string
text
n
Number of characters; a negative n keeps all but the first |n|
integer

Return value

The last n characters; with a negative n, all characters except the first |n|

Examples

Last seven characters

PostgreSQL 17.9
SELECT RIGHT('sql-academy', 7);
right
academy

All but the first four characters

PostgreSQL 17.9
SELECT RIGHT('sql-academy', -4);
right
academy

Details

If n is greater than the string length, the whole string is returned. In MySQL a negative n gives an empty string, while here it cuts characters off the start.

See also

Where to learn