Oracle 19c SQL

  1. Home
  2. Docs
  3. Oracle 19c SQL
  4. Single Row Character Functions
  5. RTRIM

RTRIM

  • RTRIM is a single row character function.
  • The RTRIM function removes all trailing characters specified in “trim-expression“.
  • RTRIM scans from from right side of a given string and removes all trailing characters until it reaches a character that is not in the “trim-expression“.
  • Return type is VARCHAR2.

Syntax : RTRIM(Str1, trim-expression)

SELECT RTRIM('  Second Example    ', ' ') Result
  FROM DUAL;

RESULT
----------------
  Second Example
  • When 2nd parameter to RTRIM is not provided, default is empty space.
  • If “trim-expression” which is the 2nd parameter if omitted, RTRIM function will remove all ending spaces in the result set.
SELECT RTRIM('  Second Example    ') Result
  FROM DUAL;

RESULT
----------------
  Second Example
  • 2nd parameter “trim-expression” can be used to trim any character from the end any number of times.
  • You can remove all characters you want to remove from the end of the input string using RTRIM function.

Following code removes all trailing “$” and “*” from the input string until it finds a character not specified as a “trim-expression” in the 2nd parameter.

SELECT RTRIM('#1Second Example##11%#**1**', '#1*') Result
  FROM DUAL;

RESULT
---------------------
#1Second Example##11%
SELECT RTRIM('#1Second Example##11%#**1**', '#1*%') Result
  FROM DUAL;

RESULT
----------------
#1Second Example
  • Specifying same character multiple times as “trim-expression” in the 2nd parameter does not makes sense though Oracle does not report any error.
  • Also Oracle does not throw any error if characters in “trim-expression” does not appear in the input string. (Here 2 and 3 are not present in input, no error is reported)
SELECT RTRIM('#1Second Example##11%#**1**', '####123*%') Result
  FROM DUAL;

RESULT
----------------
#1Second Example
  • RTRIM can be used with NUMBER data types too. Both parameters to RTRIM can be of NUMBER data type. Returns a NUMBER when dealing with NUMBER types.
SELECT RTRIM(1212145321122133111, 123) Result 
  FROM DUAL;

RESULT
-------
1212145
Was this article helpful to you? Yes No

How can we help?