> This page location: String Functions > RIGHT
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL RIGHT() Function

**Summary**: in this tutorial, you will learn how to use the PostgreSQL `RIGHT()` function to return the last `n` characters in a string.

## Introduction to the PostgreSQL RIGHT() function

The `RIGHT()` function allows you to retrieve the last n characters of a string.

Here's the basic syntax of the `RIGHT()` function:

```sql
RIGHT(string, n)
```

The PostgreSQL `RIGHT()` function requires two arguments:

- `string` is a string from which a number of the rightmost characters are returned.
- **`n`** is a positive integer that specifies the number of the rightmost characters in the string that should be returned.

The `RIGHT()` function returns the last `n` characters in a string. If `n` is negative, the `RIGHT()` function returns all characters in the string but first `|n|` (absolute) characters.

If you want to return the `n` first characters of a string, you can use the [`LEFT()`](https://neon.com/postgresql/postgresql-string-functions/postgresql-left) function.

## PostgreSQL RIGHT() function examples

Let's take some examples of using the PostgreSQL `RIGHT()` function.

### 1) Basic PostgreSQL RIGHT() function example

The following statement uses the `RIGHT()` function to get the last character in the string `'XYZ'`:

```sql
SELECT RIGHT('XYZ', 1);
```

Here is the result:

```text
 right
-------
 Z
(1 row)

```

To get the last two characters, you pass the value `2` as the second argument as follows:

```sql
SELECT RIGHT('XYZ', 2);
```

Output:

```text
 right
-------
 YZ
(1 row)
```

The following statement illustrates how to use a negative integer as the second argument:

```sql
SELECT RIGHT('XYZ', - 1);
```

In this example, the `RIGHT()` function returns all characters except for the first character.

```text
 right
-------
 YZ
(1 row)
```

### 2) Using the RIGHT() function with table data example

See the following `customer` table in the [sample database](../postgresql-getting-started/postgresql-sample-database):

![customer table](https://neon.com/postgresqltutorial/customer.png)The following statement uses the `RIGHT()` function in [`WHERE`](../postgresql-tutorial/postgresql-where) clause to get all customers whose last names end with `'son'`:

```sql
SELECT
  last_name
FROM
  customer
WHERE
  RIGHT(last_name, 3) = 'son';
```

Output:

```
  last_name
-------------
 Johnson
 Wilson
 Anderson
 Jackson
 Thompson
...
```

## Summary

- Use the PostgreSQL `RIGHT()` function to get the n rightmost characters in a string.

---

## Related docs (String Functions)

- [ASCII](https://neon.com/postgresql/postgresql-string-functions/postgresql-ascii)
- [CHR](https://neon.com/postgresql/postgresql-string-functions/postgresql-chr)
- [CONCAT](https://neon.com/postgresql/postgresql-string-functions/postgresql-concat-function)
- [CONCAT_WS](https://neon.com/postgresql/postgresql-string-functions/postgresql-concat_ws)
- [FORMAT](https://neon.com/postgresql/postgresql-string-functions/postgresql-format)
- [INITCAP](https://neon.com/postgresql/postgresql-string-functions/postgresql-initcap)
- [LEFT](https://neon.com/postgresql/postgresql-string-functions/postgresql-left)
- [LENGTH](https://neon.com/postgresql/postgresql-string-functions/postgresql-length-function)
- [LOWER](https://neon.com/postgresql/postgresql-string-functions/postgresql-lower)
- [LPAD](https://neon.com/postgresql/postgresql-string-functions/postgresql-lpad)
- [LTRIM](https://neon.com/postgresql/postgresql-string-functions/postgresql-ltrim)
- [MD5](https://neon.com/postgresql/postgresql-string-functions/postgresql-md5)
- [POSITION](https://neon.com/postgresql/postgresql-string-functions/postgresql-position)
- [REGEXP_MATCHES](https://neon.com/postgresql/postgresql-string-functions/postgresql-regexp_matches)
- [REGEXP_REPLACE](https://neon.com/postgresql/postgresql-string-functions/regexp_replace)
- [REPEAT](https://neon.com/postgresql/postgresql-string-functions/postgresql-repeat)
- [REVERSE](https://neon.com/postgresql/postgresql-string-functions/postgresql-reverse)
- [REPLACE](https://neon.com/postgresql/postgresql-string-functions/postgresql-replace)
- [RPAD](https://neon.com/postgresql/postgresql-string-functions/postgresql-rpad)
- [RTRIM](https://neon.com/postgresql/postgresql-string-functions/postgresql-rtrim)
- [SPLIT_PART](https://neon.com/postgresql/postgresql-string-functions/postgresql-split_part)
- [SUBSTRING](https://neon.com/postgresql/postgresql-string-functions/postgresql-substring)
- [TO_CHAR](https://neon.com/postgresql/postgresql-string-functions/postgresql-to_char)
- [TO_NUMBER](https://neon.com/postgresql/postgresql-string-functions/postgresql-to_number)
- [TRANSLATE](https://neon.com/postgresql/postgresql-string-functions/postgresql-translate)
- [TRIM](https://neon.com/postgresql/postgresql-string-functions/postgresql-trim-function)
- [UPPER](https://neon.com/postgresql/postgresql-string-functions/postgresql-upper)
