How LTRIM() Works in MariaDB

In MariaDB, LTRIM() is a built-in string function that returns a string with any leading space characters removed.

Syntax

The syntax goes like this:

LTRIM(str)

Where str is the string to remove any leading spaces from.

Example

Here’s a basic example:

SELECT 
    '   Caribbean Sea   ' AS "Untrimmed",
    LTRIM('   Caribbean Sea   ') AS "Trimmed";

Result:

+---------------------+------------------+
| Untrimmed           | Trimmed          |
+---------------------+------------------+
|    Caribbean Sea    | Caribbean Sea    |
+---------------------+------------------+

Here, the first column is untrimmed and the second one has been trimmed with LTRIM().

We can see that only the left part of the string is trimmed. The right part is left intact.

We can also see that the space within the string is left intact.

Null Argument

If the argument is null, the result is null:

SELECT LTRIM(null);

Result:

+-------------+
| LTRIM(null) |
+-------------+
| NULL        |
+-------------+

Oracle Mode

When not running in Oracle mode, if the result is empty (i.e. it has a length of zero) the result is an empty string.

However, when running in Oracle mode, the result is null.

Here it is in default mode (i.e. not in Oracle mode):

SELECT LTRIM('');

Result:

+-----------+
| LTRIM('') |
+-----------+
|           |
+-----------+

Now let’s switch to Oracle mode:

SET SQL_MODE=ORACLE;

And run the code again:

SELECT LTRIM('');

Result:

+-----------+
| LTRIM('') |
+-----------+
| NULL      |
+-----------+

There’s also an alternative way to do this. Instead of switching to Oracle mode, you can use LTRIM_ORACLE() as the function name.

Let’s switch back to default mode:

SET SQL_MODE=DEFAULT;

And now run LTRIM_ORACLE():

SELECT LTRIM_ORACLE('');

Result:

+------------------+
| LTRIM_ORACLE('') |
+------------------+
| NULL             |
+------------------+

Missing Argument

Calling LTRIM() without an argument results in an error:

SELECT LTRIM();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LTRIM'