Skip to content

RTRIM ​

Removes trailing characters, including spaces, from a expr string.

Syntax ​

sql
RTRIM(expr[, trim_chars])

Arguments ​

expr: A string of one of the data types.


trim_chars: One or more characters of data type STRING to be removed from the right side of expr. If no characters are specified, spaces are removed.

Return Value ​

The return value is of data type STRING.

Usage Notes ​

  • The order of characters in the trim_chars argument is not considered.
  • If multiple characters are specified as trim_chars, spaces must be explicitly included in the argument to remove spaces. Example: The expression '@ ,' would remove all trailing spaces, @-signs, and commas from the input string. The expression '@,' would only remove @-signs and commas from the input string.
  • Tab characters, newline characters, and other 'whitespace' characters must be explicitly specified.

Example 1 ​

sql
SELECT rtrim('73400#0', '0#') as result1, 
       rtrim('734#000', '0#') as result2, 
       rtrim('27340#00', '0#') as result3,
       rtrim('734è# 0 0 0', '0# ') as result4,
       rtrim('7 304è# 0 0 0', '0# ') as result5;
txt
+---------+---------+---------+---------+---------+
| RESULT1 | RESULT2 | RESULT3 | RESULT4 | RESULT5 |
+---------+---------+---------+---------+---------+
| 734     | 734     | 2734    | 734è    | 7 304è  |
+---------+---------+---------+---------+---------+

Example 2 | RTRIM and Specific Whitespace Characters ​

Tab characters, newline characters, and other 'whitespace' characters must be explicitly specified.

expr contains a tab character ('chr(9)'), which was explicitly specified in the trim_chars argument in the query and thus could be removed (expr_trimmed1), and in the query for expr_trimmed2, it was not explicitly specified and thus could not be removed.

sql
select             ('"' ||'Lorem ipsum'|| chr(9)|| '"')                as expr,
             length('"' ||'Lorem ipsum'|| chr(9)|| '"')                as expr_len,
              rtrim('"' ||'Lorem ipsum'|| chr(9)|| '"',('"'||chr(9)))  as expr_trimmed1,        
       length(rtrim('"' ||'Lorem ipsum'|| chr(9)|| '"',('"'||chr(9)))) as expr_trimmed1_len,
              rtrim('"' ||'Lorem ipsum'|| chr(9)|| '"',('"'||' '))     as expr_trimmed2,        
       length(rtrim('"' ||'Lorem ipsum'|| chr(9)|| '"',('"'||' ')))    as expr_trimmed2_len;
txt
+----------------+----------+---------------+-------------------+---------------+-------------------+
| EXPR           | EXPR_LEN | EXPR_TRIMMED1 | EXPR_TRIMMED1_LEN | EXPR_TRIMMED2 | EXPR_TRIMMED2_LEN |
+----------------+----------+---------------+-------------------+---------------+-------------------+
| "Lorem ipsum " | 14       | "Lorem ipsum  | 12                | "Lorem ipsum  | 13                |
+----------------+----------+---------------+-------------------+---------------+-------------------+