LTRIM() Function in SQL Server

The LTRIM() function in SQL Server removes all the space characters found on the left-hand side of the string. It removes the leading spaces from a string,

Syntax

The LTRIM function for SQL Server syntax is:

LTRIM(string, [trim_string])

Parameter:

  • string – The string from which the leading space character would be removed.
  • trim_string – It is an optional parameter that specifies the characters to be removed from the given string.

Returns:

The function will return the string after removing all left-hand side space characters.

The LTRIM function is applicable to the following versions of SQL Server:

  • SQL Server 2017
  • SQL Server 2016
  • SQL Server 2014
  • SQL Server 2012
  • SQL Server 2008 R2
  • SQL Server 2008
  • SQL Server 2005

SQL Server LTRIM function Example

Let’s look at some examples of LTRIM function in SQL Server.

Using LTRIM() function Example

In this example, we will demonstrate the basic use of the LTRIM function in SQL Server.

SELECT LTRIM('       w3wiki');

Output :

w3wiki

LTRIM() function with a variable Example

In this example, we will use the LTRIM function in SQL Server with a variable.

DECLARE @str VARCHAR(50)
SET @str = ' Have a nice time ahead!!'
SELECT LTRIM(@str);

Output :

Have a nice time ahead!!

Use of “trim_space” in LTRIM() function Example

In this example, we will remove specific characters from a string using the LTRIM function.

SELECT LTRIM (‘000325400’, ‘0’);

Output :

325400

Important Points About SQL Server LTRIM function

  • The LTRIM() function in SQL Server stands for “left trim” and is used to remove leading spaces (whitespace characters) from a text string.
  • It can also be used to remove specific characters from a string.
  • It helps in cleaning data by removing unnecessary spaces at the beginning of a string.

Contact Us