RTRIM()

SQL · String Functions

Theory

RTRIM(string) removes trailing spaces, the ones at the end, and leaves the start untouched. RTRIM('Asha ') returns 'Asha'. As with LTRIM, SQLite, Postgres and Oracle accept a second argument listing characters to remove, so RTRIM('Delhi..', '.') returns 'Delhi'. In MySQL use TRIM(TRAILING '.' FROM x) for anything other than spaces.

RTRIM is especially common with fixed-width data. A CHAR(10) column, or a mainframe or bank export file, pads short values with spaces to a fixed length, so 'Pune' may really be stored as 'Pune '. Some databases ignore trailing spaces when comparing, others do not, and the padding still shows up in LENGTH, in concatenation and in exported files. RTRIM before concatenating or exporting avoids gaps like 'Pune , India'. It is also used to remove trailing punctuation or separators left behind when building strings.

Why we use it

Trailing spaces are the most invisible kind of dirty data: you cannot see them on screen, yet they break joins, string building and exports. RTRIM removes them without touching leading characters that may matter.

When to use it

Use RTRIM for fixed-width or CHAR-padded values, and to drop trailing punctuation or separators. Use TRIM when both ends may be dirty, and LTRIM for the start only.

How it works

  1. Take the string (and optional characters to strip)
  2. Look at the last character
  3. Is it in the strip set (space by default)? Yes: Remove it and look at the new last one. No: Stop.
  4. The rest of the string is kept as is
  5. Return the right-trimmed string

Syntax

RTRIM(string)

Example

SELECT RTRIM('Asha   ');
-- 'Asha'

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic