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:

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:

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:

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.

Input Table

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:

Output Table

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.

Similar Reads

LTRIM() Function in SQL

The LTRIM() function in SQL is an inbuilt string function that is used to remove the leading empty spaces from the left side of our data. The LTRIM function can also be used to remove a specific character or substring from the left side of the string....

Syntax

SQL LTRIM function syntax is:...

SQL LTRIM function Working

The following diagram shows how the SQL LTRIM function affects strings in the table....

SQL LTRIM Function Examples

Now let us see some examples of LTRIM function in different use cases to understand it’s working better:...

Important Points About SQL LTRIM Function

SQL LTRIM is useful inbuilt function which can be used to remove the whitespaces on left our data in the databases. Not only whitespaces it can also remove a specific character or a subtring from the data. LTRIM function in SQL can be used on entire column/field of table to remove whitespace. LTRIM function only works for the left side of data, and will not affect the middle or right side of data....

Contact Us