SQLite trim() function

The SQLite trim() function removes specified characters at the beginning and the end of a string.

If you don’t specify the characters to remove, the trim() function will remove the space characters.

Note that the trim() function does not remove other whitespace characters by default.

Syntax

trim(string, character)Code language: SQL (Structured Query Language) (sql)

Arguments

string
The input string that you want to remove the character.

character

The character determines which characters you want to remove from the input string. It is an optional argument.

If you omit the character argument, the trim() function will remove the spaces from both ends of the input string.

Return value

The trim() function returns a new string with the specified leading and trailing characters removed. It does not change the input string.

Examples

The following statement uses the trim() function to return a string without spaces at the beginning and end of a string.

SELECT trim('   SQLite trim   ') result;Code language: SQL (Structured Query Language) (sql)

Output:

result
-----------
SQLite trim

The following example uses the trim() function to remove the USD string from a USD amount so 400USD becomes 400:

SELECT trim('400USD','USD') amount;Code language: SQL (Structured Query Language) (sql)

Output:

amount
------
400

The following example uses the trim() function to remove the $ symbol at the beginning of a USD amount:

SELECT trim('$400','$') amount;Code language: JavaScript (javascript)

Output:

amount
------
400

The following statement uses the trim() function in the UPDATE statement to remove leading and trailing spaces from artist names in the artists table:

UPDATE artists
SET Name = trim(Name);Code language: SQL (Structured Query Language) (sql)
Was this tutorial helpful ?