Oracle RTRIM() Function

Oracle RTRIM() is a built-in function that returns a string with any specified trailing characters (by default, spaces) removed.

If you need to remove characters from the left side of a string, use LTRIM(); if you need to remove characters from both the left and right sides of a string, use TRIM().

Oracle RTRIM() Syntax

Here is the syntax for the Oracle RTRIM() function:

RTRIM(str [, set ])

Parameters

str

Required. It can be a value of type CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB.

set

Optional. It is the set of characters to be removed. The default is space.

Return Value

The Oracle RTRIM() function returns a string with any specified trailing characters (by default, spaces) removed.

RTRIM() scans from the last character of str backwards and removes all characters that appear in set, until it reaches a character that is not in set, and then returns the result.

If any parameter is NULL, RTRIM() returns NULL.

Oracle RTRIM() Examples

Here are some examples that demonstrate the usage of the Oracle RTRIM() function.

Remove whitespace

This statement shows how to use the Oracle RTRIM() function to remove the whitespace on the right side of ' Hello ':

SELECT
    '   Hello   ' Origin,
    RTRIM('   Hello   ') Result
FROM dual;

Output:

ORIGIN         RESULT
______________ ___________
   Hello          Hello

Remove specified characters

This statement shows how to use the Oracle RTRIM() function to remove the _*# characters on the right side of '___***#Hello':

SELECT
    '___***#Hello___***#' Origin,
    RTRIM('___***#Hello___***#', '_*#') Result
FROM dual;

Output:

ORIGIN                 RESULT
______________________ _______________
___***#Hello___***#    ___***#Hello

NULL parameter

If any parameter is NULL, RTRIM() returns NULL.

SET NULL 'NULL';
SELECT
    RTRIM(NULL),
    RTRIM(NULL, 'ABC'),
    RTRIM('ABC', NULL)
FROM dual;

Output:

RTRIM(NULL)    RTRIM(NULL,'ABC')    RTRIM('ABC',NULL)
______________ ____________________ ____________________
NULL           NULL                 NULL

In this example, we use the statement SET NULL 'NULL'; to display NULL values as the string 'NULL'.

Conclusion

The Oracle RTRIM() function is a built-in function that returns a string with any specified trailing characters (by default, spaces) removed.