Returns a string, binary data, or array in reverse order.
Syntax
STRING|BINARY|ARRAY REVERSE(STRING|BINARY|ARRAY <value>)
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
value |
Yes | STRING, BINARY, or ARRAY | The value to reverse. Values of data types such as BIGINT, DOUBLE, DECIMAL, and DATETIME are implicitly converted to STRING before the operation. If the value cannot be converted to STRING, BINARY, or ARRAY, an error is returned. If the value is null, null is returned. |
Return value
Returns the reversed value. The return type matches the input type: STRING input returns STRING, BINARY returns BINARY, and ARRAY returns ARRAY.
Encoding behavior
By default, REVERSE operates byte by byte regardless of input type.
In BigQuery compatibility mode (SET odps.sql.bigquery.compatible=true;), the behavior differs by type:
| Input type | Default mode | BigQuery compatibility mode |
|---|---|---|
| STRING | Reversed byte by byte | Reversed by UTF-8 character |
| BINARY | Reversed byte by byte | Reversed byte by byte |
| ARRAY | Reversed byte by byte | Reversed byte by byte |
Why this matters for multi-byte characters: In default mode, REVERSE treats each byte as an independent unit. Multi-byte Unicode characters (such as characters from languages like Chinese, Japanese, or Arabic) span multiple bytes, so reversing byte by byte produces garbled results. In BigQuery compatibility mode, REVERSE treats each UTF-8 encoded Unicode code point as a single unit, which correctly reverses strings containing these characters.
Examples
Reverse a string
SELECT REVERSE('I love aliyun very much');
| Result |
|---|
hcum yrev nuyila evol I |
Return null for a null input
SELECT REVERSE(null);
| Result |
|---|
NULL |
Reverse an array
SELECT REVERSE(ARRAY(2, 1, 4, 3));
| Result |
|---|
[3, 4, 1, 2] |
Reverse binary data
SELECT REVERSE(UNHEX('FA34E10293CB42848573A4E39937F479'));
| Result |
|---|
y=F47=99=E3=A4s=85=84B=CB=93=02=E14=FA |
Reverse a string in BigQuery compatibility mode
Use this example to test multi-byte character reversal via the SQL code editor in the MaxCompute client.
Run the following steps from the bin folder of the MaxCompute client.
-
In the
binfolder, create a file namedreverse_func.sqlwith the following content:SET odps.sql.bigquery.compatible=true; SELECT REVERSE("<Emoji character>"); -
Open a command-line window and run the following command:
odpscmd -s reverse_func.sqlView the reversed result on the Result tab in Logview.
Related functions
REVERSE is a string function. For other string search and conversion functions, see String functions.