sqlcmd.net validated sql reference
beginner string-manipulation MySQL MariaDB SQL Server PostgreSQL

Repeat or Reverse a String

Use `REPEAT` to duplicate a string a given number of times, and `REVERSE` to flip its character order.

Docker-validated Not currently validation-green

Build a separator line and check a palindrome

REVERSE('racecar') = 'racecar' — it is the same forwards and backwards, so palindrome is 'yes'. REVERSE('hello') = 'olleh', which differs from the original, so palindrome is 'no'. REVERSE('level') = 'level' — another palindrome.

Rows loaded before the example query runs.
Setup
CREATE TABLE words (id INT, word VARCHAR(50));

INSERT INTO
  words
VALUES
  (1, 'racecar'),
  (2, 'hello'),
  (3, 'level');
Shared across supported engines.
SQL
SELECT
  id,
  word,
  REVERSE (word) AS reversed,
  CASE
    WHEN word = REVERSE (word) THEN 'yes'
    ELSE 'no'
  END AS palindrome
FROM
  words
ORDER BY
  id;
Returned rows for the shared example.
idwordreversedpalindrome
1racecarracecaryes
2helloollehno
3levellevelyes

REVERSE syntax is identical across MySQL, MariaDB, SQL Server, and PostgreSQL.

Generate a separator string using REPEAT

REPEAT('-', 10) produces ten hyphen characters. REPEAT('ab', 3) concatenates 'ab' three times to give 'ababab'. SQL Server's equivalent function is REPLICATE — it accepts the same arguments in the same order.

SQL Server uses REPLICATE instead of REPEAT. All other supported engines use REPEAT.

Where this command helps.

  • generating a fixed-width separator or padding string without hard-coding the repeated character
  • checking whether a string is a palindrome by comparing it to its reverse

What the command is doing.

REPEAT(str, n) concatenates str with itself n times — useful for padding, separator generation, or creating test data. REVERSE(str) returns the characters in reverse order. Both functions are supported in MySQL, MariaDB, SQL Server, and PostgreSQL. SQLite has no built-in REPEAT or REVERSE — the printf function can pad a string to a fixed width, but arbitrary repetition requires a recursive CTE.