w3resource

MySQL LTRIM() function

LTRIM() function

MySQL LTRIM() removes the leading space characters of a string passed as an argument.

This function is useful in -

  • Leading character removal: LTRIM() removes certain characters from the left side of a string.
  • Data cleaning: LTRIM() is often used to remove spaces from strings at the beginning.

Syntax:

LTRIM(str)

Argument:

Name Description
str A string whose leading space characters are to be removed.

Syntax Diagram:

MySQL LTRIM() Function - Syntax Diagram

MySQL Version: 8.0

Example of MySQL LTRIM() function

The following MySQL statement returns the string ‘ Hello’ after removing the leading space characters. In the output, the first column shows the string with leading space characters and the second column shows the string after performing LTRIM.

Code:

SELECT ' Hello', LTRIM(' Hello');

Output:

mysql> SELECT ' Hello', LTRIM(' Hello');
+---------+------------------+
| Hello  | LTRIM(' Hello') |
+---------+------------------+
|  Hello | Hello           | 
+---------+------------------+
1 row in set (0.02 sec)

Example of LTRIM() with update statement

The following MySQL statement will update the aut_name column for all the rows by removing the leading space characters (if any).

Code:

UPDATE author SET aut_name=LTRIM(aut_name);

Sample table: author


Output:

mysql> UPDATE author SET aut_name=LTRIM(aut_name);
Query OK, 0 rows affected (0.09 sec)
Rows matched: 15  Changed: 0  Warnings: 0

Example of LTRIM() with update statement using WHERE clause

The following MySQL statement will update the aut_name column by removing the leading space characters for those rows which don't belong to the ' USA'.

Code:

UPDATE author SET aut_name=LTRIM(aut_name)
WHERE country<>'USA';

Sample table: author


Output:

mysql> UPDATE author SET aut_name=LTRIM(aut_name)
    -> WHERE country<>'USA';
Query OK, 0 rows affected (0.01 sec)
Rows matched: 11  Changed: 0  Warnings: 0

Video Presentation

All String Functions (Slides presentation)

Previous: LPAD
Next: MAKE_SET



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mysql/string-functions/mysql-ltrim-function.php