// strings reference

SQL REVERSE function

Returns the string with its characters in reverse order. Its everyday use is finding the last occurrence of a character, such as the final slash in a path.

Categoría
Strings
Returns
string
In this category
13 of 172

Sintaxis

REVERSE(string)

Soporte del motor

MotorSupportNotas
SQL Server Sí Supported under this name.
PostgreSQL Sí Supported under this name.
MySQL Sí Supported under this name.
SQLite Not built in no REVERSE; a recursive CTE or an extension
Oracle Not built in REVERSE exists but is undocumented and reverses bytes, so it breaks multi-byte characters

Ejemplos

SQL Server

-- same on PostgreSQL and MySQL
SELECT REVERSE('stressed') AS dessert;  -- desserts

PostgreSQL

SELECT RIGHT(path, POSITION('/' IN REVERSE(path)) - 1) AS file_name
FROM files;

Conviene saberlo

  • SQLite has no REVERSE. Oracle's works on single-byte text but is undocumented and reverses bytes, which corrupts multi-byte UTF-8 characters.
  • Emoji: PostgreSQL and MySQL reverse whole characters. SQL Server reverses UTF-16 code units, so an emoji breaks unless the column uses a supplementary-character (_SC) collation.
  • Combining accents (an e followed by a combining acute mark) come out attached to the wrong letter on every engine, because the mark is a separate character.

Questions about REVERSE

Does Oracle have a REVERSE function?

Not officially. REVERSE('abc') returns 'cba' on Oracle, but the function is undocumented, unsupported, and reverses bytes rather than characters, so multi-byte UTF-8 text comes out corrupted. For real text, write a PL/SQL function.

Is REVERSE safe for emoji and accents?

Not fully. Combining marks end up on the wrong letter on every engine, and SQL Server splits emoji unless the collation is supplementary-character aware (_SC). Use it on codes and identifiers, not prose.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.