MySQL RTRIM Function

Created with Sketch.

MySQL RTRIM Function

Summary: in this tutorial, you will learn how to use the RTRIM() function to remove all trailing spaces of a string.

MySQL RTRIM() overview

The RTRIM() function takes a string argument and returns a new string with all the trailing space characters removed.

Here is the syntax of the RTRIM() function:

RTRIM(str)

Code language: SQL (Structured Query Language) (sql)

In this syntax, the str argument is the string that you want to remove the trailing spaces.

Note that to remove the leading spaces from a string, you use the LTRIM() function. To remove both leading and trailing spaces from a string, you use the TRIM() function.

MySQL RTRIM() function examples

Let’s take some examples of using the RTRIM() function.

A) Using MySQL RTRIM() function with a literal string

This example uses the RTRIM() function to remove all trailing spaces of the string 'MySQL ':

SELECT
RTRIM('MySQL ') result;

Code language: SQL (Structured Query Language) (sql)

Here is the result:

+--------+
| result |
+--------+
| MySQL |
+--------+
1 row in set (0.00 sec)

Code language: SQL (Structured Query Language) (sql)

B) Using MySQL RTRIM() function to update data in a table

The following example uses the RTRIM() function to remove all trailing spaces of customer names in the customerName column of the customers table from the sample database:

UPDATE
customers
SET
customerName = RTRIM(customerName);

Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use the MySQL RTRIM() function to remove all trailing spaces of a string.

Leave a Reply

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