reverse

description

Syntax

1. VARCHAR reverse(VARCHAR str)
2. ARRAY<T> reverse(ARRAY<T> arr)

The REVERSE() function reverses a string or array and returns the result.

notice

For the array type, only supported in vectorized engine

example

1. mysql> SELECT REVERSE('hello');
2. +------------------+
3. | REVERSE('hello') |
4. +------------------+
5. | olleh            |
6. +------------------+
7. 1 row in set (0.00 sec)

9. mysql> SELECT REVERSE('你好');
10. +------------------+
11. | REVERSE('你好')   |
12. +------------------+
13. | 好你              |
14. +------------------+
15. 1 row in set (0.00 sec)

17. mysql> set enable_vectorized_engine=true;

19. mysql> select k1, k2, reverse(k2) from array_test order by k1;
20. +------+-----------------------------+-----------------------------+
21. | k1   | k2                          | reverse(`k2`)               |
22. +------+-----------------------------+-----------------------------+
23. |  1   | [1, 2, 3, 4, 5]             | [5, 4, 3, 2, 1]             |
24. |  2   | [6, 7, 8]                   | [8, 7, 6]                   |
25. |  3   | []                          | []                          |
26. |  4   | NULL                        | NULL                        |
27. |  5   | [1, 2, 3, 4, 5, 4, 3, 2, 1] | [1, 2, 3, 4, 5, 4, 3, 2, 1] |
28. |  6   | [1, 2, 3, NULL]             | [NULL, 3, 2, 1]             |
29. |  7   | [4, 5, 6, NULL, NULL]       | [NULL, NULL, 6, 5, 4]       |
30. +------+-----------------------------+-----------------------------+

32. mysql> select k1, k2, reverse(k2) from array_test01 order by k1;
33. +------+-----------------------------------+-----------------------------------+
34. | k1   | k2                                | reverse(`k2`)                     |
35. +------+-----------------------------------+-----------------------------------+
36. |  1   | ['a', 'b', 'c', 'd']              | ['d', 'c', 'b', 'a']              |
37. |  2   | ['e', 'f', 'g', 'h']              | ['h', 'g', 'f', 'e']              |
38. |  3   | [NULL, 'a', NULL, 'b', NULL, 'c'] | ['c', NULL, 'b', NULL, 'a', NULL] |
39. |  4   | ['d', 'e', NULL, ' ']             | [' ', NULL, 'e', 'd']             |
40. |  5   | [' ', NULL, 'f', 'g']             | ['g', 'f', NULL, ' ']             |
41. +------+-----------------------------------+-----------------------------------+

keywords

1. REVERSE, ARRAY