RTRIM

RTRIM is a function in SQL that removes trailing spaces from a string.

RTRIM(string)

  • string: This parameter refers to the string of characters from which you want to remove extra whitespace on the right end.

Example

SELECT RTRIM('SQL ');

Output

'SQL'

Explanation

The RTRIM function in SQL is used to remove all the trailing whitespaces at the right-hand side of a given string. In the mentioned example, the function RTRIM has been applied to the string ‘SQL ’, which removes all the trailing spaces resulting in the output: ‘SQL’.

RTRIM( character_expression )

  • character_expression: A parameter that indicates the string from which SQL Server will remove trailing spaces. This expression can be of any data type that can be implicitly converted to varchar or nvarchar, otherwise, an error is returned. It cannot be a text or ntext data type.

Example

SELECT RTRIM('hello world ') AS TrimmedText;

Output

'hello world'

Explanation

The RTRIM function removed the trailing spaces from the right side of the input string.

RTRIM(character, trim_character)

  • character: The string from which trailing spaces or specified characters are to be trimmed. This must be a character literal, expression, or function.
  • trim_character: The set of characters at the end of “character” that are to be trimmed. This must also be a character literal, expression, or function. If this parameter is omitted, Oracle will trim trailing spaces by default.

Example

SELECT RTRIM('Example ') AS TrimmedString
FROM dual;

Output

TrimmedString
-------------
Example

Explanation

In the given example, Oracle’s RTRIM function removes all the trailing spaces from the string ‘Example ’ and returns the trimmed string ‘Example’.

RTRIM(text, characters)

  • text: The string from which the trailing characters need to be removed.
  • characters: The set of characters to be removed from the end of the string. If this parameter is omitted, spaces are removed by default.

Example

SELECT RTRIM('SQLite ');

Output

'SQLite'

Explanation

The RTRIM function in SQLite is used to remove all the trailing spaces from a string. In the given example, RTRIM function is applied to the string ‘SQLite ’, which has three trailing spaces at the end. The function removes these spaces and returns the string ‘SQLite’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.