MariaDB LTRIM() Function
In MariaDB, LTRIM()
is a built-in string function that returns a string with all the leading whitespaces removed.
If you want to remove trailing spaces from a string, use the RTRIM()
function.
If you want to remove both leading and trailing spaces from a string, use the TRIM()
function.
MariaDB LTRIM()
Syntax
Here is the syntax of the MariaDB LTRIM()
function:
LTRIM(str)
Parameters
str
-
Required. A string that needs to remove leading whitespaces.
If you do not provide a parameter, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LTRIM'
.
Return value
The MariaDB LTRIM()
function returns strings with leading spaces removed.
If the argument is NULL
, the LTRIM()
function will return NULL
.
MariaDB LTRIM()
Examples
To remove leading spaces from ' Hello '
, use the following statement:
SELECT
LTRIM(' Hello '),
CHAR_LENGTH(LTRIM(' Hello '));
Output:
+--------------------+---------------------------------+
| LTRIM(' Hello ') | CHAR_LENGTH(LTRIM(' Hello ')) |
+--------------------+---------------------------------+
| Hello | 6 |
+--------------------+---------------------------------+
In this example, we used the CHAR_LENGTH()
function. CHAR_LENGTH(LTRIM(' Hello '))
returned 6, which means that LTRIM()
only removed leading spaces.
Oracle mode
In Oracle mode, the LTRIM()
function returns NULL
instead of the empty string.
In default mode, the following statement will return an empty string:
SELECT LTRIM(' ');
Output:
+--------------+
| LTRIM(' ') |
+--------------+
| |
+--------------+
Use the following statement to switch to Oracle mode:
SET SQL_MODE=ORACLE;
and run the code again:
SELECT LTRIM(' ');
Output:
+--------------+
| LTRIM(' ') |
+--------------+
| NULL |
+--------------+
This behaves exactly like the LTRIM_ORACLE()
function.
Conclusion
In MariaDB, LTRIM()
is a built-in string function that returns a string with all leading whitespaces removed.