REVERSE function

The REVERSE function accepts a character expression as its argument, and returns a string of the same length, but with the ordinal positions of every logical character reversed.

This is the syntax of the REVERSE function:

REVERSE Function

REVERSE(source_string )
Element Description Restrictions Syntax
source_string Expression that evaluates to a character string Must be an expression, constant, column, or host variable of a type that can be converted to a character type Expression

The argument to the REVERSE function cannot have a user-defined data type. The built-in CHAR, LVARCHAR, NCHAR, NVARCHAR, and VARCHAR types are valid.

The REVERSE function returns a string of the same data type as its source_string argument.

If the expression that you specify as the argument evaluates to NULL, the return value is NULL.

For an argument that evaluates to string of N characters, the ordinal position p of each character in the source_string becomes (N + 1 - p) in the returned string. This inverts the sequence of characters from their original order in the source_string, so that the return value begins with the last character of the source_string, and ends with the first character of the source_string.

For example, the function expression REVERSE('Mood') returns the string dooM from the quoted-string argument. In both single-byte and multibyte code sets, only the ordinal positions are reversed, not the characters themselves. In the function expression above, 'd' does not become 'b', and each logical character in a multibyte code set (for example, utf8, or GB2312-80) is repositioned as a single logical unit.

If the argument evaluates to a single-character or to an empty source_string, the return value and the source_string are identical, as if the REVERSE function had no effect. For strings that include multiple characters, this equality is true only when the source_string is a palindrome. For character strings where MOD(N,2) = 1, the character in ordinal position (N+1)/2 has the same middle position in both the source_string and in the returned string.

In the following example, the REVERSE function reverses a quoted string argument:
SELECT REVERSE('Able was I ere I saw Elba.') FROM Mirror_Table;
The following table shows the output of this SELECT statement.
(constant)
.ablE was I ere I saw elbA