SQL LTRIM function

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

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). 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 LTRIM Function Example 1

The Sql Server LTRIM Function is used to return 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 the LTRIM function.

TIP: You can use the SQL Server CAST function 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 Function 1

The below code is used to declare the VARCHAR variable and assigning 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 next line, We used the SQL LTRIM function directly on string data

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

As you see from the below, we have some leading spaces before and after the string. The following String Function 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 Function Example 2

In this example, We are going to use the Sql LTRIM function on Column Names. Generally, we may get data with some extra spaces due to typing mistake or any other data entry issues. If you find such data, then you have to 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
SQL LTRIM Function 2

SQL LTRIM Function Example 3

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

SQL LTRIM Function 3

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

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