![]() |
VOOZH | about |
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.
Query:
SELECT RTRIM('Hello World ') AS TrimmedText;Output:
Syntax:
RTRIM( input_text,[Trim_Characters]);
OR
RTRIM(column_name) AS trimmed_name
FROM table_name;
Let's see some examples of RTRIM function in SQL and understand it's working with examples of different use cases.
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-124050First, 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:
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:
RTRIM().CONCAT() to append "World" to the trimmed string.CALL RTRIM_Example().// so the entire procedure is treated as a single statement.