PostgreSQL - RIGHT Function
Last Updated :
26 Jul, 2024
The PostgreSQL RIGHT() function, allows you to extract a specified number of characters from the right side of a string. This function can be incredibly useful for various text-processing tasks.
Let us get a better understanding of the RIGHT Function in PostgreSQL from this article.
Syntax
RIGHT(string, n)
Parameters
Let's analyze the above syntax:
- string: The input string from which you want to extract the rightmost characters.
- n: An integer specifying the number of characters to be extracted from the right end of the string.
PostgreSQL RIGHT Function Examples
Let us take a look at some of the examples of the RIGHT Function in PostgreSQL to better understand the concept.
Example 1: Retrieving the Last Character of a String
The following statement can be used to query for the last character in the string 'XYZ'.
Query:
SELECT RIGHT('XYZ', 1);
Output:

Explanation: In this example, the RIGHT() function takes the string 'XYZ' and the integer 1 as arguments. It returns the last character of the string, which is 'Z'.
Example 2: Using RIGHT() to Filter Data in a WHERE Clause
The following query uses the RIGHT() function in WHERE clause to get all customers whose last names ended with 'son' in the customer table of the sample database.
Query:
SELECT last_name
FROM customer
WHERE RIGHT(last_name, 3) = 'son';
Output:

Explanation: In this query, the RIGHT() function is used within the WHERE clause to filter the customer table. It extracts the last three characters of each 'last_name' and compares them to 'son'. The query returns all rows where the last three characters of the 'last_name' match 'son'.
Important Points About PostgreSQL RIGHT Function
- The RIGHT() function does not support negative values for the 'n' parameter.
- The RIGHT() function works not only with text and character strings but also with other data types that can be implicitly cast to text, such as 'varchar' and 'char'.
- If the 'n' value is greater than the length of the string, RIGHT() will return the entire string without raising an error.
- Trailing spaces in strings are preserved when using RIGHT(). If you need to remove trailing spaces, combine RIGHT() with the TRIM() function.
Similar Reads
PostgreSQL - TRIM Function The TRIM() function in PostgreSQL is an essential tool for removing unwanted characters from strings. Whether we're working with user inputs, formatting text, or performing data cleansing operations, TRIM() is an invaluable function for managing string data. This article will provide an in-depth loo
4 min read
PostgreSQL String Functions PostgreSQL is a powerful, open-source relational database management system that offers a rich set of functions and operators for working with string data. String manipulation is an essential task in many applications, and PostgreSQL provides a variety of built-in functions to make working with text
8 min read
PostgreSQL REVERSE() Function The REVERSE() function in PostgreSQL is a simple yet powerful tool used to reverse the order of characters in a given string. It takes one input which is a string and returns the characters in reverse order. This function is helpful when you need to transform data, run tests or validate information.
4 min read
PostgreSQL - NOW() Function The NOW() function in PostgreSQL is a powerful and essential tool for retrieving the current date and time. This is particularly useful when recording actions like adding or updating records in our database. Using PostgreSQL NOW() function, we can efficiently track when events occur, making the data
4 min read
PostgreSQL - Function Returning A Table In PostgreSQL, the ability to create functions that return tables enhances the power and flexibility of our database operations. These PostgreSQL RETURN TABLE functions allow us to execute complex queries and return structured data efficiently. In this article, we will explain the PostgreSQL functio
4 min read