PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
Home / PostgreSQL String Functions / PostgreSQL RIGHT

PostgreSQL RIGHT

The PostgreSQL RIGHT() function returns the last n characters in a string.

Syntax

The following shows the syntax of the PostgreSQL RIGHT() function:

1
RIGHT(string, n)    

Arguments

The PostgreSQL RIGHT() function requires two arguments:

1) string

is a string from which a number of the rightmost characters returned.

2) n

is a positive integer which specifies the number of the rightmost characters in the string should be returned.

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

Return value

The PostgreSQL RIGHT() function returns the last n characters in a string.

Examples

Let’s look at some examples of using the PostgreSQL RIGHT() function.

The following statement gets the last character in the string 'XYZ':

1
SELECT RIGHT('XYZ', 1);

Here is the result:

1
2
3
4
right
-------
Z
(1 row)

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

1
SELECT RIGHT('XYZ', 2);

And the result is:

1
2
3
4
right
-------
YZ
(1 row)

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

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

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

1
2
3
4
right
-------
YZ
(1 row)

See the following customer table in the sample database:

customer table

The following statement uses the RIGHT() function in WHERE clause to get all customers whose last names ended with 'son':

1
2
3
SELECT last_name
FROM customer
WHERE RIGHT(last_name, 3) = 'son';

The following picture illustrates the result:

PostgreSQL RIGHT example

Remarks

If you want to return the n first characters, please check the LEFT() function for more detailed information.

In this tutorial, you’ve learned how to use the PostgreSQL RIGHT() function to get the n rightmost characters in a string.

Previous Tutorial: PostgreSQL LEFT
Next Tutorial: PostgreSQL LPAD

PostgreSQL String Functions

  • ASCII
  • CONCAT
  • CHR
  • FORMAT
  • LEFT
  • LENGTH
  • LPAD
  • MD5
  • POSITION
  • REGEXP_MATCHES
  • REGEXP_REPLACE
  • RIGHT
  • REPLACE
  • SPLIT_PART
  • SUBSTRING
  • TRANSLATE
  • TRIM

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL CURRENT_TIMESTAMP
  • PostgreSQL CURRENT_TIME
  • PostgreSQL CURRENT_DATE
  • PostgreSQL Date Functions
  • PostgreSQL EXTRACT
  • PostgreSQL String Functions
  • PostgreSQL SPLIT_PART
  • PostgreSQL CHR
  • PostgreSQL ASCII
  • PostgreSQL Aggregate Functions

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.