RTRIM

Start of changeThe RTRIM function removes bytes from the end of a string expression based on the content of a trim expression.End of change

Read syntax diagram
>>-RTRIM--(--string-expression--+--------------------+--)------><
                                '-,--trim-expression-'      

The schema is SYSIBM.

Start of changeThe RTRIM function removes all of the characters contained in trim-expression from the end of string-expression. The search is done by comparing the binary representation of each character (which consists of one or more bytes) in trim-expression to the bytes at the end of string-expression. If string-expression is defined as FOR BIT DATA, the search is done by comparing each byte in trim-expression to the byte at the end of string-expression.End of change

string-expression
Start of changeAn expression that specifies the source string. The argument must be an expression that returns a value that is a built-in string data type that is not a LOB, or a numeric data type. If the value is not a string data type, it is implicitly cast to VARCHAR before the function is evaluated. If string-expression is not FOR BIT DATA, trim-expression must not be FOR BIT DATA.End of change
Start of changetrim-expressionEnd of change
Start of changeAn expression that specifies the characters to remove from the end of string-expression. The expression must return a value that is a built-in string data type that is not a LOB, or a numeric data type. If the value is not a string data type, it is implicitly cast to VARCHAR before the function is evaluated.

The default for trim-expression depends on the data type of string-expression:

  • A DBCS blank if string-expression is a DBCS graphic string. For ASCII, the CCSID determines the hex value that represents a DBCS blank. For example, for Japanese (CCSID 301), X'8140' represents a DBCS blank, while for Simplified Chinese, X'A1A1' represents a DBCS blank. For EBCDIC, X'4040' represents a DBCS blank.
  • A UTF-16 or UCS-2 blank (X'0020') if string-expression is a Unicode graphic string.
  • A value of X'00' if string-expression is a binary string.
  • Otherwise, a single byte blank. For EBCDIC, X'40' represents a blank. When not EBCDIC, X'20' represents a blank.
End of change
Start of change

string-expression and trim-expression must have compatible data types. If string-expression and trim-expression have different CCSID sets, trim-expression is converted to the CCSID of string-expression.

The result of the function depends on the data type of string-expression.

  • VARCHAR if string-expression is a character string. If string-expression is defined as FOR BIT DATA, the result is FOR BIT DATA.
  • VARGRAPHIC if string-expression is a graphic string.
  • VARBINARY if string-expression is a binary string.

The length attribute of the result is the same as the length attribute of string-expression.

The actual length of the result for a character or binary string is the length of string-expression minus the number of bytes removed. The actual length of the result for a graphic string is the length (in number of double byte characters) of string-expression minus the number of double byte characters removed. If all of the characters are removed, the result is an empty string (the length is zero).

The result can be null; if the argument is null, the result is the null value.

The CCSID of the result is the same as that of string-expression.

End of change
Start of change
Example: Use the RTRIM function to remove individual numbers in the second argument from the end (right side) of the first argument:
SELECT RTRIM ('123DEFG123', '321'),
			 RTRIM ('12322XYZ12322222', '123'),
			 RTRIM ('12321', '213'),
			 RTRIM ('123XYX', '321')
		FROM SYSIBM.SYSDUMMY1

The result is '123DEFG', '12322XYZ', '' (empty string - all characters removed), and '123XYX' (no characters removed).

The RTRIM function does not remove instances of '1', '2', and '3' on the left side of the string, before characters that are not '1', '2', or '3'.

Example: Use the RTRIM function to remove individual characters in the second argument from the end (right side) of the first argument:
SELECT RTRIM ('((-78.0))' , '-0.()')
   FROM SYSIBM.SYSDUMMY1 

The result is '((-78'.

Example: Use the RTRIM function to remove dollar signs and periods in the second argument from the end (right side) of the first argument:
SELECT RTRIM ('...$VAR$...', '$.')
   FROM SYSIBM.SYSDUMMY1 

The result is '...$VAR'.

End of change