REVERSE function - Amazon Redshift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

REVERSE function

The REVERSE function operates on a string and returns the characters in reverse order. For example, reverse('abcde') returns edcba. This function works on numeric and date data types as well as character data types; however, in most cases it has practical value for character strings.

Syntax

REVERSE( expression )

Argument

expression

An expression with a character, date, timestamp, or numeric data type that represents the target of the character reversal. All expressions are implicitly converted to VARCHAR strings. Trailing blanks in CHAR strings are ignored.

Return type

VARCHAR

Examples

The following examples use data from the USERS and SALES tables in the TICKIT sample database. For more information, see Sample database.

To select five distinct city names and their corresponding reversed names from the USERS table, use the following example.

SELECT DISTINCT city AS cityname, REVERSE(cityname) FROM users ORDER BY city LIMIT 5; +----------+----------+ | cityname | reverse | +----------+----------+ | Aberdeen | needrebA | | Abilene | enelibA | | Ada | adA | | Agat | tagA | | Agawam | mawagA | +----------+----------+

To select five sales IDs and their corresponding reversed IDs cast as character strings, use the following example.

SELECT salesid, REVERSE(salesid) FROM sales ORDER BY salesid DESC LIMIT 5; +---------+---------+ | salesid | reverse | +---------+---------+ | 172456 | 654271 | | 172455 | 554271 | | 172454 | 454271 | | 172453 | 354271 | | 172452 | 254271 | +---------+---------+