PostgreSQL 18 reference · String Functions
PostgreSQL RIGHT() Function
Return the last n characters of a string. A negative n returns all but the first |n| characters.
PostgreSQL 18 Since 9.1
Syntax
right(string, n)
Parameters
| Parameter | Description |
|---|---|
string |
The input text. |
n |
How many characters to keep; negative drops from the start. |
Return value
Text.
Examples
SELECT right('invoice-2026.pdf', 3) AS ext, right('ReadyCamel', -5) AS b;
Output
ext | b -----+------- pdf | Camel (1 row)
Run this on your computer — see “Get Started” for setup.
SELECT '**** ' || right('4111222233334444', 4) AS masked;
Output
masked ----------- **** 4444 (1 row)
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| PostgreSQL 9.1 | Added |
Related
LEFT()· Return the first n characters of a string. A negative n returns all but the last |n| characters.SUBSTRING()· Extract part of a string by 1-based position and length, or the part matching a regular expression.- SQL String Functions · tutorial