PostgreSQL REVERSE() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL REVERSE()
function to reverse the characters within a string.
Introduction to PostgreSQL REVERSE() function
The REVERSE()
function accepts a string and returns a new string with the order of all characters reversed.
Here’s the syntax of the REVERSE()
function:
In this syntax:
text
: The input string that you want to reverse.
The REVERSE()
function returns a string with the order of all the characters reversed.
The REVERSE()
function returns NULL
if the text
is NULL
.
MySQL REVERSE() function examples
Let’s explore some examples of using the REVERSE()
function.
1) Basic PostgreSQL REVERSE() function example
The following example uses the REVERSE()
function to reverse the string "SQL"
:
Output:
2) Using the PostgreSQL REVERSE() function with table data
We’ll use the customer
table from the sample database:
The following example uses the REVERSE()
function to reverse the first names of customers:
Output:
3) Using REVERSE() function to detect palindromes
A palindrome is a string that reads the same forward and backward such as "radar"
.
You can use the REVERSE()
function to reverse a string and then compare the reversed string with the original string to determine if it is a palindrome. For example:
First, create a new table called words
to store the words:
Second, insert some rows into the words
table:
Output:
Third, determine if a value in the word
column is a palindrome using the REVERSE()
function:
Output:
Summary
- Use the PostgreSQL
REVERSE()
function to reverse the order of characters within a string.