w3resource
MYSQL Tutorial

MySQL LTRIM() function

LTRIM() function

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

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: 5.6

Video Presentation

Example of MySQL LTRIM() function

The following MySQL statement returns the string ‘ Hellow’ 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 ' Hellow', LTRIM(' Hellow');

Sample Output:

mysql> SELECT ' Hellow', LTRIM(' Hellow');
+---------+------------------+
| Hellow  | LTRIM(' Hellow') |
+---------+------------------+
|  Hellow | Hellow           | 
+---------+------------------+
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


Sample 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


Sample 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

All String Functions

MySQL String Functions, slide presentation

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.