SQL Server > Conversion related

Trimming unnecessary space from string in SQL Server

How to trim unnecessary space from the string in SQL Server?


To trim unnecessary blank spaces from the string in SQL Server, we use LTRIM or RTRIM functions.

  • LTRIM – removes the left side spaces, if any
  • RTRIM – removes the right side spaces, if any

To remove all spaces, we can use both LTRIM and RTRIM functions.

DECLARE @myName varchar(50)
SET @myName = '           ITFunda.com     '

SELECT @myName
SELECT LTRIM(@myName), RTRIM(@myName), LTRIM(RTRIM(@myName))

Notice the output of above code snippet below.

In first case, the output is coming along with left side and right side blank space.

In the 2nd result set

  • The first column value is after removing all left side spaces from the string
  • The second column value is after removing all right side spaces from the string
  • The third column removes spaces from right side and then left side and gives the actual string without any blank space either side.
 Views: 1282 | Post Order: 81



Write for us