Summary: in this tutorial, you will learn how to use the LTRIM()
function to remove all leading spaces of a string.
MySQL LTRIM() function overview
The LTRIM()
function takes a string argument and returns a new string with all the leading space characters removed.
The following illustrates the syntax of the LTRIM()
function:
LTRIM(str)
Code language: SQL (Structured Query Language) (sql)
In this syntax, the str
argument is the string that you want to remove all the leading spaces.
If the str
is NULL, the LTRIM()
function returns NULL.
Note that to remove the trailing spaces from a string, you use the RTRIM()
function. To remove both leading and trailing spaces from a string, you use the TRIM()
function.
MySQL LTRIM() function examples
Let’s see some examples of using the LTRIM()
function.
1) Using LTRIM() function with a literal string
The following statement uses the LTRIM()
function to remove all leading spaces of the string ' MySQL Tutorial'
:
SELECT
LTRIM(' MySQL Tutorial') result;
Code language: SQL (Structured Query Language) (sql)
Here is the output:
+----------------+
| result |
+----------------+
| MySQL Tutorial |
+----------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
2) Using LTRIM() function to update data in a table
We’ll use the customers
table from the sample database for the demonstration:
The following query uses the LTRIM()
function to remove all leading spaces of the addresses in the addressLine1
column of the customers
table.
UPDATE
customers
SET
addressLine1 = LTRIM(addressLine1);
Code language: SQL (Structured Query Language) (sql)
Summary
- Use the
LTRIM()
function to remove all the leading spaces of a string.