The SQL LTRIM function removes the leading blank spaces from the input character expression.
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
SQL LTRIM Examples
The following query shows the simple usage of an LTRIM function.
/* Answer */ SELECT LTRIM(' TRIM LEADING SPACES') result;
It removes all the leading blank spaces.
The following query uses the AdventureWorks2019 database.
/* 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'