- Categories:
String & binary functions (General)
REVERSE¶
Reverses the order of characters in a string, or of bytes in a binary value.
The returned value is the same length as the input, but with the characters/bytes in reverse order. Ifsubject
is NULL, the result is also NULL.
Syntax¶
REVERSE(<subject>)
Collation details¶
No impact.
The collation of the result is the same as the collation of the input.
In languages where the alphabet contains digraphs or trigraphs (such as “Dz” and “Dzs” in Hungarian), each character in each digraph and trigraph is treated as an independent character, not as part of a single multi-character letter.
For example, languages with 2-character and 3-character letters (e.g. “dzs” in Hungarian, “ch” in Czech)are reversed based on the individual characters, not the letters. See theExamples section below for an example.
Examples¶
This example reverses a string:
SELECTREVERSE('Hello, world!');+--------------------------+| REVERSE('HELLO, WORLD!') ||--------------------------|| !dlrow ,olleH |+--------------------------+Copy
This example reverses a date:
SELECT'2019-05-22'::DATE,REVERSE('2019-05-22'::DATE)ASreversed;+--------------------+------------+| '2019-05-22'::DATE | REVERSED ||--------------------+------------|| 2019-05-22 | 22-50-9102 |+--------------------+------------+Copy
The following shows that in languages where a single letter is composed of multiple characters,REVERSE
reverses based on characters, not letters:
CREATETABLEstrings(s1VARCHARCOLLATE'en',s2VARCHARCOLLATE'hu');INSERTINTOstrings(s1,s2)VALUES('dzsa',COLLATE('dzsa','hu'));CopySELECTs1,s2,REVERSE(s1),REVERSE(s2)FROMstrings;+------+------+-------------+-------------+| S1 | S2 | REVERSE(S1) | REVERSE(S2) ||------+------+-------------+-------------|| dzsa | dzsa | aszd | aszd |+------+------+-------------+-------------+Copy