Skip to main content

RTRIM

Description

The RTRIM function is used to remove consecutive spaces or specified strings from the right side (trailing) of a string. If the second parameter is not specified, trailing spaces are removed; if specified, the function removes the specified complete string from the right side.

Syntax

RTRIM(<str>[, <rhs>])

Parameters

ParameterDescription
<str>The string to be processed. Type: VARCHAR
<rhs>Optional parameter, trailing characters to be trimmed. Type: VARCHAR

Return Value

Returns a value of type VARCHAR.

Special cases:

  • If any parameter is NULL, NULL is returned
  • If rhs is not specified, removes trailing spaces
  • If rhs is specified, removes the complete rhs string (not character-by-character)

Examples

  1. Remove trailing spaces
SELECT rtrim('ab d   ') str;
+------+
| str |
+------+
| ab d |
+------+
  1. Remove specified trailing string
SELECT rtrim('ababccaab', 'ab') str;
+---------+
| str |
+---------+
| ababcca |
+---------+
  1. UTF-8 character support
SELECT rtrim('ṭṛì ḍḍumai   ');
+---------------------------+
| rtrim('ṭṛì ḍḍumai ') |
+---------------------------+
| ṭṛì ḍḍumai |
+---------------------------+
  1. No matching suffix, return original string
SELECT rtrim('Hello World', 'xyz');
+---------------------------------+
| rtrim('Hello World', 'xyz') |
+---------------------------------+
| Hello World |
+---------------------------------+
  1. NULL value handling
SELECT rtrim(NULL), rtrim('Hello', NULL);
+-------------+------------------------+
| rtrim(NULL) | rtrim('Hello', NULL) |
+-------------+------------------------+
| NULL | NULL |
+-------------+------------------------+
  1. Empty string handling
SELECT rtrim(''), rtrim('abc', '');
+-----------+-------------------+
| rtrim('') | rtrim('abc', '') |
+-----------+-------------------+
| | abc |
+-----------+-------------------+
  1. Repeated trailing pattern removal
SELECT rtrim('abcabcabc', 'abc');
+-------------------------------+
| rtrim('abcabcabc', 'abc') |
+-------------------------------+
| |
+-------------------------------+
  1. Multiple occurrences, remove only trailing match
SELECT rtrim('HelloHelloWorld', 'Hello');
+---------------------------------------+
| rtrim('HelloHelloWorld', 'Hello') |
+---------------------------------------+
| HelloHelloWorld |
+---------------------------------------+

Keywords

RTRIM