Do You Want to Learn SQL?

Take our free eight day course, where I'll teach you in simple to understand English all you need to get started learning SQL.

-
DAYS
-
HOURS
-
MINUTES
-
SECONDS

Free Beginners SQL Course!

SQL LTRIM Function (Transact SQL)

·

·

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

More from the blog


MySQL PostgreSQL SQLite SQL Server