In MariaDB, RTRIM()
is a built-in string function that returns a string with any trailing space characters removed.
Syntax
The syntax goes like this:
RTRIM(str)
Where str
is the string to remove any trailing spaces from.
Example
Here’s a basic example:
SELECT
' Solar System ' AS "Untrimmed",
RTRIM(' Solar System ') AS "Trimmed";
Result:
+--------------------+-----------------+ | Untrimmed | Trimmed | +--------------------+-----------------+ | Solar System | Solar System | +--------------------+-----------------+
Here, the first column is untrimmed and the second one has been trimmed with RTRIM()
.
We can see that only the right part of the string is trimmed. The left part remains intact.
We can also see that the space within the string remains intact.
Null Argument
If the argument is null
, the result is null
:
SELECT RTRIM(null);
Result:
+-------------+ | RTRIM(null) | +-------------+ | NULL | +-------------+
Oracle Mode
When not running in Oracle mode, if the result is empty (i.e. it has a length of zero) the result is an empty string.
However, when running in Oracle mode, the result is null
.
Here it is in default mode (i.e. not in Oracle mode):
SELECT RTRIM('');
Result:
+-----------+ | RTRIM('') | +-----------+ | | +-----------+
Now let’s switch to Oracle mode:
SET SQL_MODE=ORACLE;
And run the code again:
SELECT RTRIM('');
Result:
+-----------+ | RTRIM('') | +-----------+ | NULL | +-----------+
There’s also an alternative way to do this. Instead of switching to Oracle mode, you can use RTRIM_ORACLE()
as the function name.
Let’s switch back to default mode:
SET SQL_MODE=DEFAULT;
And now run RTRIM_ORACLE()
:
SELECT RTRIM_ORACLE('');
Result:
+------------------+ | RTRIM_ORACLE('') | +------------------+ | NULL | +------------------+
Missing Argument
Calling RTRIM()
without an argument results in an error:
SELECT RTRIM();
Result:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'RTRIM'