SQL Server LTRIM Function


By:

The LTRIM function is used to remove the leading spaces from a string of characters or character expressions. L is for left and TRIM means to remove unwanted leading spaces.

Syntax

LTRIM(expression)

Parameters

  • expression - this is the string or expression with leading spaces that you want to left trim.

Simple LTRIM Example

The following example will LTRIM the leading characters only even though there are additional spaces in the string.

SELECT LTRIM('    What a   wonderful   world   ')
simple example T-SQL LTRIM

LTRIM Example Using Table Columns

The following example shows how to remove any leading spaces from a column.

SELECT LTRIM([AddressLine1]) as addressline
FROM [Person].[Address]
Apply LTRIM function to columns

Example with LTRIM and CONCAT

The following example show what happens if we use LTRIM and CONCAT to concatenate strings.

  • The first column "example1", the CONCAT id done first and then the LTRIM.
  • The second column "example2", the LTRIM is done first and then the CONCAT.
DECLARE @string1 varchar(30)='  this is the first message'
DECLARE @string2 varchar(30)='  this is the second message'

SELECT LTRIM(CONCAT(@string1,@string2)) example1,
       CONCAT(LTRIM(@string1),LTRIM(@string2)) example2

Example1 shows the leading spaces for string1 have been removed, but the leading spaces for string 2 remain.

Example2 shows the leading spaces for both string1 and string2 have been removed.

LTRIM combined with CONCAT

Related Articles


Last Update: 11/8/2021




Comments For This Article

















get free sql tips
agree to terms