Skip to content
Home » SQL Server LTRIM()

SQL Server LTRIM()

SQL Server LTRIM() function is used to trim leading spaces from a character string.




SYNTAX

LTRIM (inputstring)

Lets look at an example of LTRIM() function in SQL.

DECLARE @name VARCHAR(50) = '   Mr John watler' 
SELECT @name AS NAME, DATALENGTH(@name) AS NAMELENGTH

You can see above result set, the DataLenght function returns a length of string that is 17 which is after including three leading spaces but in actual the length of string is 14.

So, to get the actual length of name string after removing those leading spaces from string, you can use LTRIM() function.

Lets look at an example to see how to remove leading spaces from string using LTRIM().

DECLARE @name VARCHAR(50) = '   Mr John watler' 
SELECT @name AS NAME , DATALENGTH(@name) AS DATALENGTH , 
DATALENGTH(LTRIM(@name)) AS DATALENGTH_USING_LTRIM 

Now you can see, all the leading spaces from string are removed and now the length of name string is 14.

Also Read..

RTRIM()

DATALENGTH()

TRIM()




Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.