VOOZH about

URL: https://www.geeksforgeeks.org/sql/rtrim-function-in-sql/

⇱ SQL RTRIM() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

SQL RTRIM() Function

Last Updated : 17 Jun, 2026

The RTRIM() function is used in SQL to remove extra spaces from the right end of a string. It helps clean and standardize text data for better storage and comparison.

  • Does not remove leading (left-side) spaces.
  • Supported by major databases like MySQL, SQL Server, Oracle and Azure SQL.

Query:

SELECT RTRIM('Hello World ') AS TrimmedText;

Output:

👁 Screenshot-2025-11-21-161810
  • Removes extra spaces from the right side of the text.
  • Returns the cleaned result as TrimmedText.

Syntax:

RTRIM( input_text,[Trim_Characters]);

OR

RTRIM(column_name) AS trimmed_name
FROM table_name;
  • RTRIM(input_text, [Trim_Characters]) removes specified characters from the right side of the text.
  • RTRIM(column_name) removes trailing spaces from a column and displays it as trimmed_name.

Examples of SQL RTRIM Function

Let's see some examples of RTRIM function in SQL and understand it's working with examples of different use cases.

Example 1: Using RTRIM Function to Remove Trailing Spaces from a String

The RTRIM function in SQL is used to remove trailing spaces from a string. Here are a few examples to illustrate its usage:

Query:

SELECT
'[' || ' Geeks for Geeks ' || ']' AS Before_RTRIM,
'[' || RTRIM(' Geeks for Geeks ') || ']' AS After_RTRIM;

Output:

👁 Screenshot-2026-06-17-124050
  • Displays the original string with leading and trailing spaces as "Before RTRIM()".
  • Removes only the right-side (trailing) spaces and shows the result as "After RTRIM()".

Example 2: Using RTRIM Function on Table Column

First, we create a table GFG, with following commands in which we add names with trailing whitespaces.

SELECT
id,
'[' || name || ']' AS name,
'[' || RTRIM(name) || ']' AS trimmed_name
FROM GFG;

Output:

👁 IMAGE

Example 3: Using RTRIM Function With a Variable

This example demonstrates how the RTRIM() function is used with a variable to remove trailing spaces and return a cleaned result.

Query:

DELIMITER //

CREATE PROCEDURE RTRIM_Example()
BEGIN
-- Declare a variable
DECLARE input_string VARCHAR(15);
-- Assign a value to the variable
SET input_string = 'Hello ';
-- Use the variable in a query
SELECT CONCAT(RTRIM(input_string), ' World') AS result;
END //
DELIMITER ;

-- Call the stored procedure
CALL RTRIM_Example();

Output:

👁 image-
RTrim Function with variable
  • Creates a stored procedure that trims trailing spaces using RTRIM().
  • Uses CONCAT() to append "World" to the trimmed string.
  • Executes the procedure with CALL RTRIM_Example().
  • Changes the delimiter to // so the entire procedure is treated as a single statement.
Comment