SQLite rtrim() Function

The SQLite rtrim() function removes the longest string containing all the characters specified by the parameter (whitespace by default) from the end of a string.

You can also use ltrim() to remove specified characters from the start of a string.

Syntax

This is the syntax of the SQLite rtrim() function:

rtrim(str)

or

rtrim(str, characters)

Parameters

str

Required. a string.

characters

Optional. A string containing the characters to remove. The default is a space.

Return value

The SQLite rtrim() function removes the longest string containing all the characters specified by the parameter (a space by default) from the end side of the gaven string, and returns the character-removed string.

Examples

This example shows the basic usage of the SQLite rtrim() function:

SELECT
    length(rtrim('a  ')),
    length(rtrim('  a')),
    length(rtrim(' a '));
length(rtrim('a  '))  length(rtrim('  a'))  length(rtrim(' a '))
--------------------  --------------------  --------------------
1                     3                     2

Here:

  • We’ve only used one parameter, so rtrim() removed spaces from the end of the string.
  • To make the result look more intuitive, we use length() function to display the length of the trimed string.

Let’s use rtrim() to remove specified characters from the end of a string:

SELECT rtrim('xxyHELLOzxy', 'xyz');
rtrim('xxyHELLOzxy', 'xyz')
---------------------------
xxyHELLO

Here, since we specified the characters xyz to be deleted, xxy in the end of xxyHELLOzxy is removed.