SQL String Functions

SQL REVERSE Function: Reverse a String (All Databases)

SQL REVERSE function to reverse string characters. Covers MySQL, PostgreSQL, SQL Server, Oracle with palindrome check examples.

Mar 12, 20263 min read

Overview

REVERSE flips a string — "Hello" becomes "olleH". Useful for palindrome checks, suffix-based searches, and data transformations.

MySQL / SQL Server

SELECT REVERSE('Hello');  -- 'olleH'

-- Check for palindromes:
SELECT word,
  CASE WHEN LOWER(word) = LOWER(REVERSE(word))
    THEN 'Palindrome' ELSE 'Not' END AS result
FROM words;

-- Search by suffix (find .com emails):
SELECT * FROM users
WHERE REVERSE(email) LIKE REVERSE('%.com');

PostgreSQL

SELECT REVERSE('Hello');  -- 'olleH'

-- Palindrome check:
SELECT word FROM words
WHERE LOWER(word) = LOWER(REVERSE(word));

-- Reverse with array (alternative):
SELECT array_to_string(ARRAY(
  SELECT chr FROM regexp_split_to_table('Hello', '') AS chr
  ORDER BY ordinality DESC
), '');

Oracle

-- Oracle has REVERSE but only for RAW types.
-- For VARCHAR, use:
SELECT REVERSE('Hello') FROM DUAL;  -- Works in 12c+

-- Older versions — custom approach:
SELECT LISTAGG(ch) WITHIN GROUP (ORDER BY pos DESC)
FROM (
  SELECT SUBSTR('Hello', LEVEL, 1) AS ch, LEVEL AS pos
  FROM DUAL
  CONNECT BY LEVEL <= LENGTH('Hello')
);

Skip the Syntax Lookup

Instead of memorizing REVERSE syntax for each database, describe what you need in plain English and let AI2SQL generate the correct query.

Try AI2SQL Free

No credit card required

Frequently Asked Questions

How do I reverse a string in SQL?

Use REVERSE(string) in MySQL, PostgreSQL, and SQL Server. Oracle 12c+ also supports REVERSE for varchar.

When would I use REVERSE in SQL?

Common uses: palindrome checks, suffix-based searching (reverse both strings and use LIKE), and certain data transformation tasks.

Can AI2SQL reverse strings?

Yes. AI2SQL can generate REVERSE queries and more complex string manipulation for your database.

Generate SQL from Plain English

Stop looking up syntax. Describe what you need and AI2SQL writes the query.

Try AI2SQL Free

No credit card required