I have trailing spaces in a column in a SQL Server table called Company Name
.
All data in this column has trailing spaces.
I want to remove all those, and I want to have the data without any trailing spaces.
The company name is like "Amit Tech Corp "
I want the company name to be "Amit Tech Corp"
If you are using SQL Server (starting with vNext) or Azure SQL Database then you can use the below query.
For other SQL SERVER Database you can use the below query.
LTRIM - Removes spaces from the left
example:
select LTRIM(' test ') as trim
='test '
RTRIM - Removes spaces from the right
example:
select RTRIM(' test ') as trim
=' test'
SQL Server does not support for Trim() function.
But you can use LTRIM() to remove leading spaces and RTRIM() to remove trailing spaces.
can use it as LTRIM(RTRIM(ColumnName)) to remove both.
I had the same problem after extracting data from excel file using ETL and finaly i found solution there :
https://www.codeproject.com/Tips/330787/LTRIM-RTRIM-doesn-t-always-work
hope it helps ;)
Well, it depends on which version of SQL Server you are using.
In SQL Server 2008 r2, 2012 And 2014 you can simply use
TRIM(CompanyName)
SQL Server TRIM Function
In other versions you have to use
set CompanyName = LTRIM(RTRIM(CompanyName))
To remove Enter:
To remove Tab:
Well here is a nice script to TRIM all varchar columns on a table dynamically: