The SQL LTRIM function removes the leading blank spaces from the input character expression.

Description

The LTRIM function returns a character expression after removing all the blank spaces from the left side of the input expression.

SQL LTRIM Usage Notes

The input expression should be explicitly convertible to a varchar value. It can be a character or a binary value but cannot be an image or a text value. However, the cast function can be used to explicitly convert the input data type to varchar.

The return type of the LTRIM function is varchar or nvarchar

Syntax

LTRIM (input_expression)

SQL LTRIM Examples

The following query shows the simple usage of an LTRIM function.

SELECT LTRIM(' TRIM LEADING SPACES') result;
/* Answer */
SELECT LTRIM('   TRIM LEADING SPACES') result;

It removes all the leading blank spaces.

The following query uses the AdventureWorks2019 database.

SELECT LTRIM(AddressLine1) Address FROM Person.Address
/* Answer */
SELECT LTRIM(AddressLine1) Address
FROM Person.Address

This query removes all the leading blank spaces in the AddressLine1 column values.

IF you’re hard pressed for why you would want to use this function consider this scenario:

The former DBA imported data “right justified” text into a CHAR(20) column names State. You now need to query this data. reliably. If you simply cast ‘ TEXAS’ as VARCHAR the spaces continue to vex you!

To get around this you can write the following to retrieve all cities in Texas:

 SELECT City
 FROM City
 WHERE LTRIM(STATE) = 'TEXAS' 

See Also

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Are YOU READY TO START LEARNING SQL SERVER, BUT DON'T KNOW WHERE TO START?

Nothing is worse than, being excited to learn a new tool but not knowing where to start, wasting time learning the wrong features, and being overwhelmed .

But it doesn't have to be this way.

I'm Putting together a free email course to help you get started learning SQL Server.

Name*
Email*
>