w3resource

MySQL RTRIM() function

RTRIM() function

MySQL RTRIM() removes the trailing spaces of a string.

This function is useful in -

  • Trailing space removal: It allows us to remove trailing spaces from a string.
  • Trailing character removal: RTRIM() can be used to remove a specified set of characters from the right side of a string.
  • Data cleansing: To remove unwanted characters or spaces from strings, RTRIM() is often used.

Syntax:

RTRIM(str)

Argument:

Name Description
str A string whose trailing spaces are to be removed.

Syntax Diagram:

MySQL RTRIM() Function - Syntax Diagram

MySQL Version: 8.0

Example: MySQL RTRIM() function

The following MySQL statement returns a string after removing all the trailing spaces of the argument string 'w3resource'.

Code:

SELECT RTRIM('w3resource ');

Output:

mysql> SELECT RTRIM('w3resource ');
+----------------------+
| RTRIM('w3resource ') |
+----------------------+
| w3resource           | 
+----------------------+
1 row in set (0.01 sec)

Example of MySQL RTRIM() function using table

The following MySQL statement returns the name of the authors with removing trailing spaces from the publisher table and home city of those authors. The WHERE condition is used to make sure that authors only those who belong to the USA are retrieved.

Code:

SELECT RTRIM(aut_name),home_city 
FROM author 
WHERE country='USA';

Sample table: author


Output:

mysql> SELECT RTRIM(aut_name),home_city 
    -> FROM author 
    -> WHERE country='USA';
+-----------------+-----------+
| RTRIM(aut_name) | home_city |
+-----------------+-----------+
| Thomas Merton   | New York  | 
| Nikolai Dewey   | Atlanta   | 
| Joseph Milton   | Houston   | 
| Butler Andre    | Florida   | 
+-----------------+-----------+
4 rows in set (0.00 sec)

Video Presentation:

All String Functions (Slides presentation)

Previous: RPAD
Next: SOUNDEX



Follow us on Facebook and Twitter for latest update.