Updated July 6, 2023
Introduction to LTRIM() in SQL
The Structured Query Language (SQL) comes with the privilege of string functions, which are used chiefly for manipulating the strings, and LTRIM() is an SQL inbuilt string function that is used to remove the presence of any leading spaces or empty spaces from the left-hand side in a string and also the LTRIM() function can be used to remove specified characters or substring from the left-hand side of the given string.
In the above syntax, the ‘string’ can be a variable or column or literal string or a character expression from which the empty spaces or characters are trimmed from the left-hand side.
How does the LTRIM() function works in SQL?
Let us have a table with a column with some leading empty spaces for the column values that need removal. Usually, these kinds of issues with leading spaces in a string can be handled using SQL string functions. The LTRIM() function is an inbuilt function that helps in removing these spaces from the left-hand side of the string. Though this function removes all the leading spaces, we can also remove the specific characters from the left-hand side of the given string by using the function LTRIM(string, substring).
So this function removes the leading spaces and is useful when we want to remove or trim a given substring from the left-hand side of the original string. The LTRIM() function only removes leading spaces in a string and leaves any other spaces within the string intact. For example, if the string consists of two words with space between them or any trailing spaces present, the LTRIM() does not remove the trailing spaces or space between them.
Examples of LTRIM() in SQL
Let us take a few examples to see how LTRIM() works:
The LTRIM() function removes the leading empty spaces from the “Hello World” string.
SELECT LTRIM(' Hello World') as example;
We can see from the above that earlier, few leading spaces were present in the string ‘Hello World.’ The result shows that using the LTRIM() string function has removed the extra spaces from the left-hand side of the string.
If, in addition to the empty spaces from the left side of the string, there also exist empty spaces on the right-hand side or in between the string, the spaces in between and the spaces at the right-hand side do not get removed by using LTRIM() function.
SELECT LTRIM(' Hello World ') as example;
The above statement will not remove the empty spaces from the right-hand side of the input string or space in between and only removes the empty spaces from the left-hand side of the given string, as mentioned below.
If we want to specify the characters to be removed or if we want to remove a substring from the original or given string, then we can also achieve that by using the LTRIM().
SELECT LTRIM('99866', '9') as example;
In the above example, we can see that, as specified in the query, all the leading 9s should be removed from the string 99866; the result is 866.
In the below statement, the string contains 9s both at the leading and trailing positions. But using the LTRIM() function will only remove all the leading 9s but not the 9s from the right-hand side, as shown below in the result.
SELECT LTRIM('9986699', '9') as example;
In the below statement, the specified string or the substring 98612 is to be trimmed from the input or given string, and it is done using LTRIM() as below.
SELECT LTRIM('99866abcde', '98612') as example;
Similarly, a set of specified characters also can be trimmed from the leading positions of the input string as below.
SELECT LTRIM('abcdefabgh', 'abcd') as example;
The result above clearly shows that the function only trimmed the ‘abcd’ substring from the left side of the input string.
Let us take the example of the below table CUSTOMERS.
Let us consider that the table has leading empty spaces for some columns. Also, the column AGE contains ‘&’ added to the age values of the customers.
In the above table, the first name, i.e., the F_NAME column, may contain some empty spaces. We can remove the leading empty spaces when using the below query needs it.
SELECT LTRIM(F_NAME) as result FROM CUSTOMERS;
The result of the above query will remove the empty spaces from the left-hand side of the column values of F_NAME, as shown below.
We can see that the column AGE has ‘&’ added to its values. This can be removed by using the LTRIM() function as below.
SELECT LTRIM(AGE, ‘&’) as result FROM CUSTOMERS;
The result of the above query will have all the ‘&’ removed from the leading positions of the values in the column AGE.
LTRIM() is an inbuilt string function in SQL that helps remove the leading white spaces from the string and the substring from the left-hand side of the input or given string. It is essential to have a good understanding of this function.
We hope that this EDUCBA information on “LTRIM() in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.