How the RTRIM() function works in Mariadb?

The RTRIM() function is a string function that removes all trailing spaces from a string.

Posted on

The RTRIM() function is a string function that removes all trailing spaces from a string. It is a built-in function in MariaDB and can be used in any SQL statement that supports string functions.

Syntax

The syntax for the RTRIM() function is as follows:

RTRIM(str)

The str argument is the string from which you want to remove trailing spaces. It can be a string literal, a variable, or a column expression.

Examples

Example 1: Remove trailing spaces from a string literal

The following query removes trailing spaces from the string literal " This is a string ":

SELECT RTRIM('   This is a string   ');

The output of the query is as follows:

This is a string

Example 2: Remove trailing spaces from a variable

The following query removes trailing spaces from the variable @str:

SET @str = '   This is a string   ';
SELECT RTRIM(@str);

The output of the query is as follows:

This is a string

Example 3: Remove trailing spaces from a column expression

The following query removes trailing spaces from the name column in the employees table:

SELECT RTRIM(name)
FROM employees;

The output of the query will be a list of all the names in the employees table, with any trailing spaces removed.

The following functions are related to the RTRIM() function:

  • The LTRIM() function removes all leading spaces from a string.
  • The TRIM() function removes all leading and trailing spaces from a string.

Conclusion

The RTRIM() function is a useful tool for removing trailing spaces from strings. It can be used to improve the readability of data and to make data more consistent.