MariaDB RTRIM() Function

In MariaDB, RTRIM() is a built-in string function that returns a string with all the leading whitespaces removed.

If you want to remove leading spaces from a string, use the LTRIM() function.

If you want to remove both trailing and trailing spaces from a string, use the TRIM() function.

MariaDB RTRIM() Syntax

Here is the syntax of the MariaDB RTRIM() function:

RTRIM(str)

Parameters

str

Required. String that need to remove trailing whitespaces.

If you do not provide a parameter, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'RTRIM'.

Return value

The MariaDB RTRIM() function returns the string with trailing spaces removed.

If the argument is NULL, the RTRIM() function will return NULL.

MariaDB RTRIM() Examples

To remove trailing spaces from ' Hello ', use the following statement:

SELECT
  RTRIM('   Hello '),
  CHAR_LENGTH(RTRIM('   Hello '));

Output:

+--------------------+---------------------------------+
| RTRIM('   Hello ') | CHAR_LENGTH(RTRIM('   Hello ')) |
+--------------------+---------------------------------+
|    Hello           |                               8 |
+--------------------+---------------------------------+

In this example, we use the CHAR_LENGTH() function. CHAR_LENGTH(RTRIM(' Hello ')) returned 8, which means that RTRIM() only removed trailing spaces.

Oracle mode

In Oracle mode, the RTRIM() function returns NULL instead of the empty string.

In default mode, the following statement will return an empty string:

SELECT RTRIM('   ');

Output:

+--------------+
| RTRIM('   ') |
+--------------+
|              |
+--------------+

Use the following statement to switch to Oracle mode:

SET SQL_MODE=ORACLE;

and run the code again:

SELECT RTRIM('   ');

Output:

+--------------+
| RTRIM('   ') |
+--------------+
| NULL         |
+--------------+

This behaves exactly like the RTRIM_ORACLE() function.

Conclusion

In MariaDB, RTRIM() is a built-in string function that returns a string with all trailing whitespaces removed.