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.
REVERSE(<seq>)
| Parameter | Description |
|---|---|
<seq> | The string or array to reverse. Type: VARCHAR or ARRAY |
Returns the reversed sequence of the same type as the input:
Special cases:
SELECT REVERSE('hello');
+------------------+ | REVERSE('hello') | +------------------+ | olleh | +------------------+
SELECT REVERSE(['hello', 'world']);
+-----------------------------+ | REVERSE(['hello', 'world']) | +-----------------------------+ | ["world", "hello"] | +-----------------------------+
SELECT REVERSE(NULL);
+---------------+ | REVERSE(NULL) | +---------------+ | NULL | +---------------+
SELECT REVERSE(''), REVERSE([]);
+-------------+-------------+ | REVERSE('') | REVERSE([]) | +-------------+-------------+ | | [] | +-------------+-------------+
SELECT REVERSE('A'), REVERSE(['single']);
+--------------+--------------------+ | REVERSE('A') | REVERSE(['single']) | +--------------+--------------------+ | A | ["single"] | +--------------+--------------------+
SELECT REVERSE('12345'), REVERSE('!@#$%');
+------------------+------------------+ | REVERSE('12345') | REVERSE('!@#$%') | +------------------+------------------+ | 54321 | %$#@! | +------------------+------------------+
SELECT REVERSE('ṭṛì ḍḍumai'), REVERSE('ḍḍumannàri');
+-----------------------+------------------------+ | REVERSE('ṭṛì ḍḍumai') | REVERSE('ḍḍumannàri') | +-----------------------+------------------------+ | iamuḍḍ ìṛṭ | irànnaumuḍḍ | +-----------------------+------------------------+
SELECT REVERSE('Hello123'), REVERSE('test@email.com');
+---------------------+--------------------------+ | REVERSE('Hello123') | REVERSE('test@email.com') | +---------------------+--------------------------+ | 321olleH | moc.liame@tset | +---------------------+--------------------------+
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"] | +---------------------------+------------------------+
SELECT REVERSE('level'), REVERSE('12321');
+------------------+-------------------+ | REVERSE('level') | REVERSE('12321') | +------------------+-------------------+ | level | 12321 | +------------------+-------------------+
The REVERSE function is used to reverse the order of characters in a string or the order of elements in an array.
REVERSE( <seq> )
| Parameter | Description |
|---|---|
<seq> | The string or array whose order needs to be reversed. |
Returns the string or array with the reversed order. Special cases:
SELECT reverse('hello');
+------------------+ | REVERSE('hello') | +------------------+ | olleh | +------------------+
SELECT reverse(['hello', 'world']);
+-----------------------------+ | reverse(['hello', 'world']) | +-----------------------------+ | ["world", "hello"] | +-----------------------------+