Oracle RTRIM

Oracle RTRIM() function removes all characters that appear in a specified set from the right end of a string.

Syntax

The following illustrates the syntax of the Oracle RTRIM() function:

RTRIM(trim_source,[set])
Code language: SQL (Structured Query Language) (sql)

Arguments

The RTRIM() function accepts two arguments:

1) trim_source

is the string which the characters that appear in the set will be removed.

2) set

is one or more characters that should be removed from the right end of the trim_source string

The set argument is optional. If you omit it when calling the RTRIM() function, it will default to a single space. In other words, the RTRIM() function will remove the spaces from the right end of the trim_source by default.

The data types of both trim_source and set can be one of the following data types: CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB, or NCLOB.

Return value

The RTRIM() function returns a string with all characters that appear in the set removed from the right end of the string.

The data type of the result returned string is VARCHAR2 if  source_string is a character value or LOB if source_string is a LOB value.

Examples

The following statement removes all spaces at the right end of a string 'ABC ':

SELECT RTRIM( 'ABC ' ) FROM dual;
Code language: SQL (Structured Query Language) (sql)

Here is the result:

'ABC'
Code language: SQL (Structured Query Language) (sql)

As you can see, two spaces at the end of the string were removed.

See the following example:

SELECT RTRIM( 'ABC12345543', '345' ) FROM dual;
Code language: SQL (Structured Query Language) (sql)

The result is:

ABC12
Code language: SQL (Structured Query Language) (sql)

For every character in the set ('3','4','5'), the RTRIM() function removed the right-most occurrences of each from the string.

Let’s see the products table in the sample database:

products table

The following statement returns products whose names end with V1, V2, etc.

SELECT product_name FROM products WHERE product_name LIKE '%V_' ORDER BY product_name;
Code language: SQL (Structured Query Language) (sql)
Oracle RTRIM - Product Names

You can use the RTRIM() function to remove the version e.g., V1, V2, etc., from the product names as follows:

SELECT product_name, RTRIM(product_name,'V12345679') short_name FROM products WHERE product_name LIKE '%V_' ORDER BY product_name;
Code language: SQL (Structured Query Language) (sql)
Oracle RTRIM - Example

In this tutorial, you have learned how to use the Oracle RTRIM() function to remove unwanted characters from the right end of a string.

Was this tutorial helpful?