Db2 RTRIM

Summary: in this tutorial, you will learn how to use the Db2 RTRIM() function to remove specific characters from the end of a string.

Db2 RTRIM() function overview

Here is the basic syntax RTRIM() function:

RTRIM(input_string, [trim_string])
Code language: SQL (Structured Query Language) (sql)

The RTRIM() function removes all characters contained in the trim_string from the end of the input_string.

If you skip the trim_string argument, the RTRIM() function will remove the trailing blanks from the input_string.

Note that Db2 uses the binary representation of each character in the trim_string to compare with the bytes at the end of the input_string.

Db2 RTRIM() function examples

Let’s take some examples of using the RTRIM() function.

1) Using Db2 RTRIM() function to remove trailing blanks from a string example

The following example uses the RTRIM() function to remove all the trailing blanks from the string 'Db2 LTRIM ':

SELECT RTRIM('Db2 RTRIM ') result FROM SYSIBM.SYSDUMMY1;
Code language: SQL (Structured Query Language) (sql)

The following is the result set:

RESULT ------------ Db2 RTRIM
Code language: SQL (Structured Query Language) (sql)

2) Using Db2 RTRIM() function to remove a specific string from the end of a string example

This example uses the RTRIM() function to remove the string '123' from the string 'abc123' string:

SELECT RTRIM('abc123','123') result FROM SYSIBM.SYSDUMMY1;
Code language: SQL (Structured Query Language) (sql)

The following illustrates the output:

RESULT ------ abc
Code language: SQL (Structured Query Language) (sql)

In this tutorial, you have learned how to use the Db2 RTRIM() function to remove a specified string from the end of a string.

Was this tutorial helpful ?