EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial LTRIM() in SQL
 

LTRIM() in SQL

Priya Pedamkar
Article byPriya Pedamkar

Updated July 6, 2023

LTRIM()-in-SQL

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

LTRIM(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:

Example #1

The LTRIM() function removes the leading empty spaces from the “Hello World” string.

SELECT LTRIM(' Hello World') as example;

Output:

LTRIM () IN SQL 1

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.

Example #2

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.

Output:

LTRIM () IN SQL 2

Example #3

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;

Output:

LTRIM () IN SQL 3

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.

Example #4

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;

Output:

 will only remove all the leading 9s

Example #5

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;

Output:

LTRIM () IN SQL 5

Similarly, a set of specified characters also can be trimmed from the leading positions of the input string as below.

Example #6

SELECT LTRIM('abcdefabgh', 'abcd') as example;

Output:

remove the empty spaces from the left hand side

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.

LTRIM () IN SQL 7JPG

Example #7

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.

Output:

remove the empty spaces from the left hand side

Example #8

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.

Output:

all the ‘&’ removed from the leading positions

Conclusion

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.

Recommended Articles

We hope that this EDUCBA information on “LTRIM() in SQL” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Cheat Sheet MySQL
  2. Hive Built-in Functions
  3. Oracle String Functions
  4. T-SQL String Functions 

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW