How the LTRIM() Function Works in MySQL

In MySQL, the LTRIM() function trims whitespace from the beginning of a string.

Syntax

The syntax goes like this:

LTRIM(str)

Where str is the string to trim.

Example

Here’s a basic example of removing whitespace from the start of a string:

SELECT LTRIM(' Tree') AS Trimmed;

Result:

+---------+
| Trimmed |
+---------+
| Tree    |
+---------+

The leading space is removed.

To demonstrate the effect better, here’s what it looks like when compared to the untrimmed string:

SELECT 
  LTRIM(' Tree') AS Result
  UNION
  SELECT ' Tree';

Result:

+--------+
| Result |
+--------+
| Tree   |
|  Tree  |
+--------+

So the first one has been trimmed and the second one hasn’t.

Multiple Spaces

If you have multiple leading spaces, all of them are trimmed:

SELECT 
  LTRIM('      Tree') AS Result
  UNION
  SELECT '      Tree';

Result:

+------------+
| Result     |
+------------+
| Tree       |
|       Tree |
+------------+

Other Spaces

This function only trims leading space. Any other space is left alone:

SELECT 
  LTRIM(' New Zealand       ') Trimmed,
  ' New Zealand       ' Untrimmed;

Result:

+--------------------+---------------------+
| Trimmed            | Untrimmed           |
+--------------------+---------------------+
| New Zealand        |  New Zealand        |
+--------------------+---------------------+

In this example, space between each word is left intact, and trailing space is also left intact on both strings. Only the the first string has its leading space trimmed.