PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References
PostgreSQL Tutorial PostgreSQL Advanced PostgreSQL Database Account Management PostgreSQL References

PostgreSQL RIGHT() Function



The PostgreSQL RIGHT() function is used to extract a substring from a string, starting from the right-most character.

Syntax

RIGHT(string, n)

Parameters

string Required. Specify the string to extract from.
n Required. Specify the number of characters to extract.
  • If this parameter exceeds the length of the string, this function will return string.
  • If this parameter is negative, this function will return the string after removing the first |n| characters.

Return Value

Returns the substring extracted from specified string.

Example 1:

The example below shows the usage of RIGHT() function.

SELECT RIGHT('AlphaCodingSkills.com', 1);
Result: 'm'

SELECT RIGHT('AlphaCodingSkills.com', 4);
Result: '.com'

SELECT RIGHT('AlphaCodingSkills.com', 21);
Result: 'AlphaCodingSkills.com'

SELECT RIGHT('AlphaCodingSkills.com', 50);
Result: 'AlphaCodingSkills.com'

SELECT RIGHT('Alpha Coding Skills', 6);
Result: 'Skills'

SELECT RIGHT('AlphaCodingSkills.com', -5);
Result: 'CodingSkills.com'

Example 2:

Consider a database table called Employee with the following records:

PhoneNumberEmpIDAddress
+33-1479961011Grenelle, Paris, France
+31-2011503192Geuzenveld, Amsterdam, Netherlands
+86-10997324583Yizhuangzhen, Beijing, China
+65-672348244Yishun, Singapore
+81-3577990725Koto City, Tokyo, Japan

In the query below, the RIGHT() function is used to exclude the country code from the PhoneNumber column records.

SELECT *, RIGHT(PhoneNumber, - 4) AS ContactNumber 
FROM Employee;

This will produce the result as shown below:

PhoneNumberEmpIDAddressContactNumber
+33-1479961011Grenelle, Paris, France147996101
+31-2011503192Geuzenveld, Amsterdam, Netherlands201150319
+86-10997324583Yizhuangzhen, Beijing, China1099732458
+65-672348244Yishun, Singapore67234824
+81-3577990725Koto City, Tokyo, Japan357799072

❮ PostgreSQL Functions