Skip to main content

REVERSE

Description

The REVERSE function is used to reverse the order of an input sequence. For string arguments, it returns a string with the character order reversed; for array arguments, it returns an array with the element order reversed. This function reverses by character and can correctly handle UTF-8 multi-byte characters.

Syntax

REVERSE(<seq>)

Parameters

ParameterDescription
<seq>The string or array to reverse. Type: VARCHAR or ARRAY

Return Value

Returns the reversed sequence of the same type as the input:

  • String input: Returns VARCHAR type, a string with reversed character order
  • Array input: Returns ARRAY type, an array with reversed element order

Special cases:

  • If parameter is NULL, returns NULL
  • If string is empty, returns empty string
  • If array is empty, returns empty array
  • A single-character string remains the same after reversal

Examples

  1. Basic string reversal
SELECT REVERSE('hello');
+------------------+
| REVERSE('hello') |
+------------------+
| olleh |
+------------------+
  1. Array element reversal
SELECT REVERSE(['hello', 'world']);
+-----------------------------+
| REVERSE(['hello', 'world']) |
+-----------------------------+
| ["world", "hello"] |
+-----------------------------+
  1. NULL value handling
SELECT REVERSE(NULL);
+---------------+
| REVERSE(NULL) |
+---------------+
| NULL |
+---------------+
  1. Empty string and empty array
SELECT REVERSE(''), REVERSE([]);
+-------------+-------------+
| REVERSE('') | REVERSE([]) |
+-------------+-------------+
| | [] |
+-------------+-------------+
  1. Single character and single element
SELECT REVERSE('A'), REVERSE(['single']);
+--------------+--------------------+
| REVERSE('A') | REVERSE(['single']) |
+--------------+--------------------+
| A | ["single"] |
+--------------+--------------------+
  1. Numbers and special characters
SELECT REVERSE('12345'), REVERSE('!@#$%');
+------------------+------------------+
| REVERSE('12345') | REVERSE('!@#$%') |
+------------------+------------------+
| 54321 | %$#@! |
+------------------+------------------+
  1. UTF-8 multi-byte characters
SELECT REVERSE('ṭṛì ḍḍumai'), REVERSE('ḍḍumannàri');
+-----------------------+------------------------+
| REVERSE('ṭṛì ḍḍumai') | REVERSE('ḍḍumannàri') |
+-----------------------+------------------------+
| iamuḍḍ ìṛṭ | irànnaumuḍḍ |
+-----------------------+------------------------+
  1. Mixed character types
SELECT REVERSE('Hello123'), REVERSE('test@email.com');
+---------------------+--------------------------+
| REVERSE('Hello123') | REVERSE('test@email.com') |
+---------------------+--------------------------+
| 321olleH | moc.liame@tset |
+---------------------+--------------------------+
  1. Multi-element array
SELECT REVERSE([1, 2, 3, 4, 5]), REVERSE(['a', 'b', 'c']);
+---------------------------+------------------------+
| REVERSE([1, 2, 3, 4, 5]) | REVERSE(['a', 'b', 'c']) |
+---------------------------+------------------------+
| [5, 4, 3, 2, 1] | ["c", "b", "a"] |
+---------------------------+------------------------+
  1. Palindrome test
SELECT REVERSE('level'), REVERSE('12321');
+------------------+-------------------+
| REVERSE('level') | REVERSE('12321') |
+------------------+-------------------+
| level | 12321 |
+------------------+-------------------+

Description

The REVERSE function is used to reverse the order of characters in a string or the order of elements in an array.

Syntax

REVERSE( <seq> )

Parameters

ParameterDescription
<seq>The string or array whose order needs to be reversed.

Return Value

Returns the string or array with the reversed order. Special cases:

  • If any Parameter is NULL, NULL will be returned.

Examples

SELECT reverse('hello');
+------------------+
| REVERSE('hello') |
+------------------+
| olleh |
+------------------+
SELECT reverse(['hello', 'world']);
+-----------------------------+
| reverse(['hello', 'world']) |
+-----------------------------+
| ["world", "hello"] |
+-----------------------------+