MySQL LTRIM Function
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.
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.
A) Using MySQL 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)
B) Using MySQL LTRIM()
function to update data in a table
See the following customers
table from the sample database.
This example 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)
In this tutorial, you have learned how to use the LTRIM()
function to remove all the leading spaces of a string.