RTRIM

The RTRIM function removes Start of changeany of the specified charactersEnd of change from the end of an expression.

Read syntax diagramSkip visual syntax diagramRTRIM (string-expression,trim-expression)

Start of changeThe RTRIM function removes all of the characters that are contained in trim-expression from the end of string-expression. The collating sequence does not affect the search. If the string-expression is defined as FOR BIT DATA or is a binary data type, 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
An expression that returns a value of any built-in numeric, Start of changedatetime,End of change or string data type. 1 A numeric or datetime argument is cast to a character string before evaluating the function. For more information about converting numeric Start of changeor datetimeEnd of change to a character string, see VARCHAR.
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 of any built-in numeric, datetime, or string data type. A numeric or datetime argument is cast to a character string before evaluating the function.
When trim-expression is not specified, the data type of the string-expression determines the default value used:
  • Hexadecimal zero (X'00') if the argument is a binary string.
  • DBCS blank if the argument is a DBCS graphic string.
  • UTF-16 or UCS-2 blank if the first argument is a Unicode graphic string.
  • UTF-8 blank if the first argument is a UTF-8 character string.
  • Otherwise, a SBCS blank.
End of change

Start of changeThe value for string-expression and the value for trim-expression must have compatible data types. For more information about data type compatibility, see Assignments and comparisons. If string-expression and trim-expression have different CCSIDs, trim-expression is converted to the CCSID of string-expression.End of change

The data type of the result depends on the data type of string-expression:

Data type of string-expression Data type of the Result
CHAR or VARCHAR VARCHAR
CLOB CLOB
GRAPHIC or VARGRAPHIC VARGRAPHIC
DBCLOB DBCLOB
BINARY or VARBINARY VARBINARY
BLOB BLOB

The length attribute of the result is the same as the length attribute of string-expression. Start of changeThe actual length of the result for character or binary strings is the length of string-expression minus the number of bytes removed. The actual length of the result for graphic strings is the length of string-expression minus the number of graphic characters removed.End of change If all characters are removed, the result is an empty string.

Start of changeIf either argument can be null, the result can be null; if any argument is null, the result is the null value.End of change

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

Examples

  • Example 1: Use the RTRIM function when the host variable HELLO is defined as CHAR(9) and has a value of
     'Hello    '
    VALUES RTRIM(:HELLO)
    The result is 'Hello'. When a trim-expression is not specified only blanks are removed.
  • Example 2: Use the RTRIM function to remove individual numbers in the trim-expression from the end (right side) of the string-expression.
    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)
    '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 3: Use the RTRIM function to remove the characters specified in the trim-expression from the end of the string-expression.
    VALUES RTRIM('...$VAR$...', '$.')
    The result is '...$VAR'.
  • Example 4: Use the RTRIM function to remove the characters specified in the trim-expression from the end of the string-expression.
    VALUES RTRIM('((-78.0) )', '-0. ()')
    The result is '((-78'. When removing characters and blanks, you must include a blank in the trim-expression.
1 The RTRIM function returns the same results as: STRIP(expression,TRAILING)