Open In App

SQL LTRIM() Function

Last Updated : 20 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The SQL LTRIM() function is an essential tool used in data cleaning and manipulation tasks. This function helps remove unwanted leading spaces or specific characters from the left side of a string or string expression. It's commonly used to tidy up data by eliminating unnecessary spaces or characters that may have been inadvertently added during data entry.

In this guide, we’ll provide an in-depth look at the SQL LTRIM() function, its syntax, how it works, and examples of common use cases.

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.

The LTRIM function is primarily used for data cleaning purposes where the data administrator uses this function to remove unnecessary characters from the database in one go.

Syntax:

SQL LTRIM function syntax is:

LTRIM(Input_String, [Trim_Characters])

Parameters

  • Input_String: String from which you want to remove leading spaces.
  • Trim_Characters: [optional] specified characters you want to remove.

Note: If we don't specify any characters to remove, the LTRIM function removes the white spaces for data cleaning and manipulation in SQL.

SQL LTRIM function Working

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

LTRIM Workflow
Workflow of LTRIM function

The function LTRIM in SQL analyzes the input string from the left side and removes any empty spaces or the mentioned character found on that string. The function does not alter the original string, it returns a new string where the leading whitespaces are removed. 

This function doesn't deal with whitespaces present between the strings. For example if the string ' JOHN' was written as ' JO HN' then only the leading white spaces on the left side will be removed by this function and the spaces in between will remain as they were earlier.

SQL LTRIM Function Examples

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

Example 1: Remove Leading White Spaces Using LTRIM function

In this example, we will remove the leading white spaces from a string using SQL LTRIM function.

Query:

Select LTRIM('     GeeksforGeeks.') AS trimmedString;

Output:

remove leading white spaces using ltrim function
Output

Example 2: Remove Specific Characters Using LTRIM Function

In this example, we will remove specific characters using LTRIM function.

Query:

SELECT LTRIM('----GeeksforGeeks', '-') AS TrimmedString;

Output:

remove specific characters using ltrim function
Output

Example 3: Remove a Substring from a String Using LTRIM Function

In this example, we will remove a substring from a string using the LTRIM function.

Query:

SELECT LTRIM('GeeksforGeeks', 'Geeks') AS TrimmedString;

Output:

remove substring using ltrim function
Output

Example 4: Use LTRIM on a Table Column

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
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
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.

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.

Conclusion

The SQL LTRIM() function is an invaluable tool for data cleaning and string manipulation. It helps remove unwanted leading spaces or characters, ensuring that your data remains clean and consistent. Whether you’re working with user input, cleaning up data from external sources, or preparing data for analysis, the LTRIM() function can make your tasks easier and more efficient.


Next Article

Similar Reads