SQL LTRIM function

The SQL Server LTRIM function is a String Function used to remove the empty spaces from the Left hand side of a character expression. This SQL LTRIM function will return VARCHAR or NVARCHAR, and its syntax is shown below.

SELECT LTRIM(Character_Expression)
FROM [Source]

Character_Expression: Please specify the valid Expression on which you want to remove leading empty spaces (performing left trimming). The SQL Server LTRIM Function will remove the leading blanks from the left side of this expression. This Character_Expression can be a variable, constant, or column of any data type, except image, text, and ntext.

SQL Server LTRIM Function Example

The SQL LTRIM Function returns the character expression after removing the empty spaces from the left side of the given expression. The following query will show multiple ways to use this function.

TIP: You can use the CAST to convert the Character_Expression explicitly

DECLARE @i VARCHAR(50)
SET @i = '     Tutorial Gateway'

SELECT LTRIM(@i) AS Result;

SELECT LTRIM('     Hello T-SQL') AS Result;  

SELECT LTRIM('     Free SQL Tutorials at Tutorial Gateway        ') AS Result;
SQL LTRIM Example 1

The below code is used to declare the VARCHAR variable and assign the string data.

DECLARE @i VARCHAR(50)
SET @i = '     Tutorial Gateway'

From the above statement, you can see that we have some leading spaces before the string ‘ Tutorial Gateway’. The following statement will remove those extra spaces from the string variable.

SELECT LTRIM(@i) AS Result;

In the following line, We used the SQL LTRIM function directly on string data.

SELECT LTRIM('     Hello T-SQL') AS Result;

As you see below we have some leading spaces before and after the string. The following String method line will remove the extra spaces from only the left side of the specified expression.

SELECT LTRIM('     Free SQL Tutorials at Tutorial Gateway        ') AS Result;

LTRIM Example 2

In this example, we will use the Sql Server LTRIM function on Column Names. Generally, we may get data with some extra spaces due to typing mistakes or any other data entry issues.

If you find such data, you must use this LTRIM or RTRIM to trim those extra spaces.

SELECT TOP 10 [ProductKey]
      ,LTRIM([EnglishProductName]) AS [ProductName]
      ,[Color]
      ,[ModelName]
      ,LTRIM([EnglishDescription]) AS [Description]
  FROM [AdventureWorksDW2014].[dbo].[DimProduct]
  WHERE [EnglishDescription] IS NOT NULL
LTRIM Example 2

SQL LTRIM Function Example 3

When you are concatenating strings, you should be very careful with extra spaces. In this example, we will use the LTRIM function along with CONCAT. For this, We are going to use the below shown data.

Source Table 3

The following statement will remove white spaces from the left hand side of the [FirstName] column and [LastName] column.

SELECT LTRIM([FirstName]) + ' ' + LTRIM([LastName]) AS [FullName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[HireDate]
  FROM [Employee]
SQL LTRIM Function 4
Categories SQL