RTRIM
The RTRIM function removes any of the specified characters from the end of an expression.
The 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.
- string-expression
- An expression that returns a value of any built-in numeric, datetime, 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 or datetime to a character string, see VARCHAR.
- trim-expression
- An 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.
The 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.
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. The 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. If all characters are removed, the result is an empty string.
If either argument can be null, the result can be null; if any argument is null, the result is the null value.
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 '
The result is 'Hello'. When a trim-expression is not specified only blanks are removed.VALUES RTRIM(:HELLO)
- Example 2: Use the RTRIM function to remove individual
numbers in the trim-expression from the end (right side) of
the string-expression.
The result is:SELECT RTRIM ('123DEFG123', '321'), RTRIM ('12322XYZ12322222', '123'), RTRIM ('12321', '213'), RTRIM ('123XYX', '321') FROM SYSIBM.SYSDUMMY1
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'.'123DEFG' '12322XYZ' '' (empty string - all characters removed) '123XYX' (no characters removed)
- Example 3: Use the RTRIM function to remove the characters
specified in the trim-expression from the end of the string-expression.
The result is '...$VAR'.VALUES RTRIM('...$VAR$...', '$.')
- Example 4: Use the RTRIM function to remove the characters
specified in the trim-expression from the end of the string-expression.
The result is '((-78'. When removing characters and blanks, you must include a blank in the trim-expression.VALUES RTRIM('((-78.0) )', '-0. ()')