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
| Parameter | Description |
|---|---|
<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
- Basic string reversal
SELECT REVERSE('hello');
+------------------+
| REVERSE('hello') |
+------------------+
| olleh |
+------------------+
- Array element reversal
SELECT REVERSE(['hello', 'world']);
+-----------------------------+
| REVERSE(['hello', 'world']) |
+-----------------------------+
| ["world", "hello"] |
+-----------------------------+
- NULL value handling
SELECT REVERSE(NULL);
+---------------+
| REVERSE(NULL) |
+---------------+
| NULL |
+---------------+
- Empty string and empty array
SELECT REVERSE(''), REVERSE([]);
+-------------+-------------+
| REVERSE('') | REVERSE([]) |
+-------------+-------------+
| | [] |
+-------------+-------------+
- Single character and single element
SELECT REVERSE('A'), REVERSE(['single']);
+--------------+--------------------+
| REVERSE('A') | REVERSE(['single']) |
+--------------+--------------------+
| A | ["single"] |
+--------------+--------------------+
- Numbers and special characters
SELECT REVERSE('12345'), REVERSE('!@#$%');
+------------------+------------------+
| REVERSE('12345') | REVERSE('!@#$%') |
+------------------+------------------+
| 54321 | %$#@! |
+------------------+------------------+
- UTF-8 multi-byte characters
SELECT REVERSE('ṭṛì ḍḍumai'), REVERSE('ḍḍumannàri');
+-----------------------+------------------------+
| REVERSE('ṭṛì ḍḍumai') | REVERSE('ḍḍumannàri') |
+-----------------------+------------------------+
| iamuḍḍ ìṛṭ | irànnaumuḍḍ |
+-----------------------+------------------------+
- Mixed character types
SELECT REVERSE('Hello123'), REVERSE('test@email.com');
+---------------------+--------------------------+
| REVERSE('Hello123') | REVERSE('test@email.com') |
+---------------------+--------------------------+
| 321olleH | moc.liame@tset |
+---------------------+--------------------------+
- 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"] |
+---------------------------+------------------------+
- 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
| Parameter | Description |
|---|---|
<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"] |
+-----------------------------+