MySQL LTRIM Function | Remove Leading Spaces from String

LTRIM is an inbuilt Function in MySQL which removes unwanted leading spaces from a string in MySQL. Data present in tables might not always what we expect. There can be unwanted spaces leading the strings. LTRIM Function removes those unwanted spaces. In this article, we will discuss the MySQL LTRIM Function. Also, we will discuss a few examples to demonstrate it’s usage.

In order to trim unwanted characters leading or trailing the string, you can use MySQL TRIM Function.

Syntax

LTRIM(string)

Parameters

The function expects only one parameter. You need to pass the string which you need to TRIM of unwanted leading spaces.

Return Value

The MySQL LTRIM Function returns the original string after removing unwanted leading spaces from the string.

MySQL LTRIM Function Diagram
MySQL LTRIM Function

Examples

Let’s discuss few examples for using the LTRIM Function

Example 1: SELECT Query

mysql> SELECT LTRIM('    Bringing Knowledge Together') as trimmedString;
+-----------------------------+
| trimmedString               |
+-----------------------------+
| Bringing Knowledge Together |
+-----------------------------+

The LTRIM Function trims the extra spaces leading the string. You can also use it to remove unwanted spaces from a column of a table. Notice the extra spaces in user description in the table below:

mysql> select * from user_description where user_name = 'Chandler';
+-----------+-------------------------------------------------------------------------------------------------------+
| user_name | user_description                                                                                      |
+-----------+-------------------------------------------------------------------------------------------------------+
| Chandler  |     Chandler Muriel Bing is a real character from the NBC sitcom Friends, portrayed by Matthew Perry. |
+-----------+-------------------------------------------------------------------------------------------------------+

We can remove the extra spaces while selecting the column using LTRIM Function.

mysql> select user_name, LTRIM(user_description) from user_description where user_name = 'Chandler';
+-----------+---------------------------------------------------------------------------------------------------+
| user_name | LTRIM(user_description)                                                                           |
+-----------+---------------------------------------------------------------------------------------------------+
| Chandler  | Chandler Muriel Bing is a real character from the NBC sitcom Friends, portrayed by Matthew Perry. |
+-----------+---------------------------------------------------------------------------------------------------+

Example 2: UPDATE Query

Similarly, we can UPDATE existing rows in a table and remove extra spaces using UPDATE Query.

mysql> update user_description set user_description = LTRIM(user_description);
Query OK, 1 row affected (0.07 sec)
Rows matched: 3  Changed: 1  Warnings: 0



mysql> select * from user_description where user_name = 'Chandler';
+-----------+---------------------------------------------------------------------------------------------------+
| user_name | user_description                                                                                  |
+-----------+---------------------------------------------------------------------------------------------------+
| Chandler  | Chandler Muriel Bing is a real character from the NBC sitcom Friends, portrayed by Matthew Perry. |
+-----------+---------------------------------------------------------------------------------------------------+

You can observe that the MySQL TRIM Function updates the column and removes extra spaces leading the string.

Conclusion

We discussed the MySQL LTRIM Function in this article. It is used to remove extra spaces leading the string. Similarly, you can use the RTRIM Function to remove spaces trailing the string. Alternatively, you can also use MySQL TRIM Function to trim any unwanted character from both ends.

Read more about LTRIM Function on MySQL Official Documentation.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *