MySQL RTRIM Function | Remove Trailing Spaces from String

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.

MySQL RTRIM Function Diagram

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.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *