SQL RTRIM function
Removes the longest run of the given characters (spaces by default) from the end of a string.
- Category
- Strings
- Returns
string- In this category
- 135 of 172
Syntax
RTRIM(string [, characters])
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Yes | The characters argument needs SQL Server 2022; before that, spaces only. |
| PostgreSQL | Yes | Supported under this name. |
| MySQL | Yes | One argument only; trims spaces. TRIM(TRAILING x FROM s) strips a repeated string. |
| SQLite | Yes | Supported under this name. |
| Oracle | Yes | Supported under this name. |
Examples
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;
Good to know
- 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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.