Last Updated : 23 Jul, 2025
SQL RTRIM function is a string function that removes spaces from the right side of a character or string. RTRIM or Right Trim function is used in data cleaning and manipulation in SQL.
In this example, we will learn the basics of the RTRIM function, and learn how it works with examples of different use cases.
RTRIM() Function in SQLSQL RTRIM() function allows us to remove the trailing empty spaces from the string. Trailing white spaces are the redundant white spaces present at the right end of the string. For example, In "GFG ", there are 5 empty white spaces at the right end of the string.
If there are redundant white spaces at the beginning of the string. For example: " GFG", there are 5 whitespaces at the beginning of the string", these are called leading whitespace.
RTRIM function works in: MySQL 4.0, SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse, Oracle, and all other major database systems.
Syntax:
Examples of SQL RTRIM FunctionRTRIM( input_text);
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.
Example 1: Using RTRIM Function to Remove Trailing Spaces from a StringThe RTRIM
function in SQL is used to remove trailing spaces from a string. Here are a few examples to illustrate its usage:
SELECT (' Geeks for Geeks ') as "Before RTRIM()", RTRIM(' Geeks for Geeks ') as "After RTRIM()";Output:
As we can see, the whitespaces in the right end of the string are removed, and the left side spaces remain as it is.
RTRIM() function Example 2: Using RTRIM Function on Table ColumnSuppose we have a table named Students with id and name. RTRIM() function will trim the trailing spaces in the given column.
First, we create a table GFG, with following commands.
MySQL
CREATE TABLE GFG(
id integer,
name varchar(40));
INSERT INTO GFG VALUES
(1,"kiran "),
(2," navya ");
Output:
Output of GFG TABLE.
Now we will use RTRIM function on column name.
SELECT name as "Before RTRIM()", RTRIM(name) as "AFTER RTRIM()" from gfg;Output:
The above query results in two columns, the first column represents the name column before using the RTRIM() function and the second column represents the values of name column after applying the RTRIM() function. As we can see, the trailing spaces are removed and width of the second column "AFTER TRIM()" is shortened.
Output after applying RTRIM() function. Example 3: Using RTRIM Function With a VariableDELIMITER // 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:
We declare a variable in stored procedure and assign it a value. We changed delimiter to "//" to consider procedure as one statement. Then we use CONCAT function to concatenate declared variable and "World".We have also use RTRIM function on declare variable in CONCAT function and As we can see in results, RTRIM method removed whitespace s from the declared variable.
Key Takeaways About SQL RTRIM FunctionConclusion
- RTRIM() function removes the trailing spaces in the given string or removes trailing spaces for all the values present in a specific column in the table.
- It allows only one parameter i.e. either a string or a column having values of string type.
- It is supported in all major database systems.
- When used in SELECT statements, the function improves the readability of results by eliminating unnecessary white spaces.
The SQL RTRIM() function is a valuable tool for removing trailing spaces in strings, making it essential for data cleaning and preparation tasks. By improving the readability and consistency of your data, it enhances both data integrity and query performance.
With the practical examples provided, you now understand how to use the RTRIM() function in various scenarios, whether it's cleaning up text data from a column, using it in stored procedures, or combining it with other string functions. When applied thoughtfully, the RTRIM() function can help you maintain a clean, consistent database that’s easier to work with and analyze.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4