The SQL LTRIM is one of the SQL String Function, which is used to remove the empty spaces from the Left hand side of a character expression. In this article we will show you, How to use/write LTRIM in SQL Server with example.
The basic syntax of the SQL Server LTRIM Function is as 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). Sql Server LTRIM Function will remove the leading blanks from left side of this expression, and this Character_Expression can be a variable, constant, or column of any data type, except image, text, and ntext.
Return Type: This ltrim function will return VARCHAR or NVARCHAR
TIP: You can use the SQL Server CAST function to explicitly convert the Character_Expression
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 LTRIM function.
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;
Below lines of code is used to declare 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’. Following statement will remove those extra spaces from the string variable. We also assigned new name to that result as ‘Result’ using ALIAS Column.
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;
You can see from the below statement, we have some leading spaces before and after the string. Following statement 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 and 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 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
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]