SQL LTRIM Function Examples
Now let us see some examples of LTRIM function in different use cases to understand it’s working better:
Remove Leading White Spaces Using LTRIM function Example
In this example, we will remove the leading white spaces from a string using SQL LTRIM function.
Query:
Select LTRIM(' w3wiki.') AS trimmedString;
Output:
Remove Specific Characters Using LTRIM function Example
In this example, we will remove specific characters using LTRIM function.
Query:
SELECT LTRIM('----w3wiki', '-') AS TrimmedString;
Output:
Remove a Substring from a String Using LTRIM function Example
In this example, we will remove a substring from a string using the LTRIM function.
Query:
SELECT LTRIM('w3wiki', 'Geeks') AS TrimmedString;
Output:
Use LTRIM on a Table Column Example
Let us consider the following table where the names of the users are stored with whitespaces. If we want to remove these whitespaces then we can use the LTRIM in SQL to remove them in one go.
To create this table, write the following SQL query:
CREATE TABLE Names (
Name VARCHAR(45)
);
INSERT INTO Names (Name) VALUES
(' Alice'), (' Bob'), (' Charlie'), (' David'), (' Eve');
Now to remove whitespace from this column, we can use the SQL query:
SELECT LTRIM(name) FROM users AS TrimmedNames;
Output:
After executing the LTRIM function on the column of the above table, you can observe that the leading whitespaces are removed in one go from all the records of the table.
SQL LTRIM() Function
The SQL LTRIM function removes the leading space from the left of a string or string expression.
Contact Us