Tuesday, 10 July 2018

Mysql LTRIM() function

Mysql LTRIM() function

MySQL LTRIM() removes the leading space characters of a string passed as an argument.
Syntax:
LTRIM(str)
Argument
NameDescription
strA string whose leading space characters are to be removed.
MySQL Version: 5.6
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

0 comments:

Post a Comment