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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

right(string, n)

Parameters

ParameterDescription
string The input text.
n How many characters to keep; negative drops from the start.

Return value

Text.

Examples

From the right
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.

Mask all but the last digits
SELECT '**** ' || right('4111222233334444', 4) AS masked;

Output

  masked
-----------
 **** 4444
(1 row)

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PostgreSQL 9.1Added

Was this page helpful?