Open In App

SQL RTRIM() Function

Last Updated : 20 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 SQL

SQL 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:

RTRIM( input_text);

OR

RTRIM(column_name) AS trimmed_name
FROM table_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:

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
RTRIM() function

Example 2: Using RTRIM Function on Table Column

Suppose 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:

gfg table created
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.

Screenshot-2023-12-26-155409
Output after applying RTRIM() function.

Example 3: Using RTRIM Function With a Variable

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:

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 Function

  • 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.

Conclusion

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.


Next Article

Similar Reads