RTRIM is an inbuilt Function in MySQL which removes unwanted trailing spaces from a string in MySQL. Data present in tables might not always what we expect. There can be unwanted spaces trailing the strings. RTRIM Function removes those unwanted spaces. In this article, we will discuss the MySQL RTRIM Function. Also, we will discuss a few examples to demonstrate it’s usage.
In order to trim unwanted characters leading or trailing the string, you can use MySQL TRIM Function.
Syntax
RTRIM(string)
Parameters
The MySQL RTRIM expects only one parameter. You need to pass the string which you need to TRIM of unwanted trailing spaces.
Return Value
The MySQL RTRIM Function returns the original string after removing unwanted leading spaces from the string.

Examples
Let’s discuss few examples for using the RTRIM Function
Example 1: SELECT Query
mysql> SELECT RTRIM('Bringing Knowledge Together ') as trimmedString; +-----------------------------+ | trimmedString | +-----------------------------+ | Bringing Knowledge Together | +-----------------------------+
The RTRIM Function trims the extra spaces trailing the string. You can also use it to remove unwanted spaces from a column of a table. Notice the extra spaces in user description in the table below:
mysql> select * from user_description where user_name = 'Chandler'; +-----------+--------------------------------------------------------------------------------------------------------+ | user_name | user_description | +-----------+--------------------------------------------------------------------------------------------------------+ | Chandler | Chandler Muriel Bing is a real character from the NBC sitcom Friends, portrayed by Matthew Perry. | +-----------+--------------------------------------------------------------------------------------------------------+
We can remove the extra spaces while selecting the column using RTRIM Function.
mysql> select user_name, RTRIM(user_description) from user_description where user_name = 'Chandler'; +-----------+---------------------------------------------------------------------------------------------------+ | user_name | RTRIM(user_description) | +-----------+---------------------------------------------------------------------------------------------------+ | Chandler | Chandler Muriel Bing is a real character from the NBC sitcom Friends, portrayed by Matthew Perry. | +-----------+---------------------------------------------------------------------------------------------------+
Example 2: UPDATE Query
Similarly, we can UPDATE existing rows in a table and remove extra spaces using UPDATE Query.
mysql> update user_description set user_description = RTRIM(user_description); Query OK, 1 row affected (0.17 sec) Rows matched: 3 Changed: 1 Warnings: 0 mysql> select * from user_description where user_name = 'Chandler'; +-----------+---------------------------------------------------------------------------------------------------+ | user_name | user_description | +-----------+---------------------------------------------------------------------------------------------------+ | Chandler | Chandler Muriel Bing is a real character from the NBC sitcom Friends, portrayed by Matthew Perry. | +-----------+---------------------------------------------------------------------------------------------------+
You can observe that the function updates the column and removes extra spaces trailing the string.
Conclusion
We discussed the MySQL RTRIM Function in this article. It is used to remove extra spaces trailing the string. Similarly, you can use the LTRIM Function to remove spaces leading the string. Alternatively, you can also use MySQL TRIM Function to trim any unwanted character from both ends.
Read more about RTRIM Function on MySQL Official Documentation.

Vishesh is currently working as an Intermediate Software Engineer with Orion Health, New Zealand. He graduated with a Masters in Information Technology from the University of Auckland in 2021. With more than 4 years of work experience, his expertise includes Java, Python, Machine Learning, PHP, Databases, Design and Architecture.