Function Reference

RIGHT

Returns the last n characters of a string.

PostgreSQL 17.5
RIGHT(string, n)

Parameters

stringtext
Source string
ninteger
Number of characters, can be negative

Return value

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

Examples

Last seven characters

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

All but the first four characters

PostgreSQL 17.5
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