Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trimming text strings in SQL Server 2008

I have a table in a SQL Server 2008 database. This table has a nvarchar(256) column called 'Name'. Unfortunately, the values in this field have extra spaces included. For instance the name 'Bill' is actually stored as 'Bill ' in the table.

I want to update all of the records in this table to remove the extra spaces. However, I was surprised to learn that SQL does not have a TRIM function.

How do I update all of the records at once to remove the extra spaces?

Thank you!

like image 976
user208662 Avatar asked Aug 15 '10 11:08

user208662


People also ask

How do I trim a string in SQL Server?

SQL Server TRIM() FunctionThe TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

What is left trim and right trim in SQL?

The SQL Server TRIM() functions can be used to remove both leading and trailing spaces from strings. LTRIM and RTRIM are two variants of this function that focus on leading (LEFT) and trailing (RIGHT) spaces respectively.

What is the equivalent of trim in SQL Server?

By default, the TRIM function removes the space character from both the start and the end of the string. This behavior is equivalent to LTRIM(RTRIM(@string)) .


1 Answers

You do have an RTRIM and an LTRIM function. You can combine them to get the trim function you want.

UPDATE Table SET Name = RTRIM(LTRIM(Name)) 
like image 109
Ronald Wildenberg Avatar answered Sep 19 '22 20:09

Ronald Wildenberg