// strings reference

SQL RTRIM function

Removes the longest run of the given characters (spaces by default) from the end of a string.

Kategorie
Strings
Returns
string
In this category
135 of 172

Syntax

RTRIM(string [, characters])

Engine-Unterstützung

EngineSupportNotizen
SQL Server Ja The characters argument needs SQL Server 2022; before that, spaces only.
PostgreSQL Ja Supported under this name.
MySQL Ja One argument only; trims spaces. TRIM(TRAILING x FROM s) strips a repeated string.
SQLite Ja Supported under this name.
Oracle Ja Supported under this name.

Beispiele

Any engine

SELECT RTRIM('padded   ') AS trimmed;  -- padded

PostgreSQL

SELECT RTRIM('report.pdfxx', 'x') AS trimmed;  -- report.pdf

MySQL

SELECT TRIM(TRAILING '0' FROM code) AS trimmed
FROM skus;

Gut zu wissen

  • The character-set argument removes any trailing characters from the set: RTRIM('12.000', '0.') is '12', but RTRIM('12.3400', '0.') is '12.34' because trimming stops at the 4.
  • PostgreSQL, Oracle, SQLite and SQL Server 2022+ accept the set argument; MySQL's RTRIM takes one argument and removes spaces only.
  • CHAR(n) columns are padded with spaces to full width, so RTRIM is the usual companion when concatenating CHAR values.

Questions about RTRIM

Why does RTRIM('12.000', '0.') also remove the dot?

The second argument is a set, applied until a character outside it appears. That makes it handy for trailing decimal zeros, but it also turns '120.000' into '12'.

Do CHAR columns need RTRIM before comparison?

Not for = in SQL Server or PostgreSQL, which ignore the padding. They do for concatenation, LIKE patterns, and length checks.

How is RTRIM different from TRIM?

TRIM works on both ends (and can target LEADING or TRAILING); RTRIM is the trailing-only form, and every engine spells it the same.

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

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// 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.