How SQLite Ltrim() Works

The SQLite ltrim() function trims whitespace or other characters from the left of a string.

More precisely, it returns a copy of the string that you provide as an argument, with the left part trimmed of either whitespace, or other characters that you specify.

Syntax

You can call it with either one or two arguments.

ltrim(X)
ltrim(X,Y)
  • If you provide one argument, this is the string to trim. In this case, whitespace is trimmed (if any) from the left of the string.
  • If you provide two arguments, the second argument contains the characters that are to be removed from the left of the string.

Trim Whitespace

Here’s an example to demonstrate how to trim whitespace from the left of the string.

SELECT ltrim(' The String');

Result:

ltrim(' The String')
--------------------
The String                

Note that only the left space is trimmed. The middle space remains intact, as would any space to the right of the string if any existed.

Here it is without the ltrim() function:

SELECT ' The String';

Result:

' The String'
-------------
 The String            

In this case, there’s still a space to the left of the string.

Multiple Spaces

If there are multiple spaces to the left of the string, ltrim() trims all of them.

SELECT 
  '    The String',
  ltrim('    The String');

Result:

'    The String'  ltrim('    The String')
----------------  -----------------------
    The String    The String                      

Trim Other Characters

As mentioned, ltrim() accepts an optional second argument that allows you to specify which character/s to trim from the string.

Here’s an example of using that syntax.

SELECT ltrim('===IMPORTANT===', '=');

Result:

IMPORTANT===            

This example highlights the fact that the right part of the string is left intact.

Below is another example. This time I specify more than one character to trim.

SELECT ltrim('!===***IMPORTANT***===!', '!=*');

Result:

IMPORTANT***===!           

The characters don’t need to be in the order that I provide in the argument. Here’s the same example, except that I switch the characters around in the second argument.

SELECT ltrim('!===***IMPORTANT***===!', '*=!');

Result:

IMPORTANT***===!           

Trim Just the Right Part or Both Sides of the String

You can also use rtrim() to trim just the right part of the string, and trim() to trim both sides.