Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to Trim all the values in a column in a single statement?

Tags:

sql

tsql

trim

I've a table in a (MS) SQL database which has an Id column (identity, int) and a Name column (varchar(250)). However, the values in the name column contain (fairly random) leading and trailing spaces as I think they were cut and pasted from "something else" (no idea what!).

Is it possible in T-SQL to do the following:

update MyTable set Name = trim(name) 

and have it update all the Name columns with the trimmed value?

like image 663
BlueChippy Avatar asked Oct 18 '11 07:10

BlueChippy


People also ask

How do I trim an entire column in SQL?

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.

How do you trim values in a column?

Click in the first cell of the Trim column. On the Formulas tab, click the Text dropdown menu in the Functions group and select TRIM.

How do you use Ltrim and Rtrim in SQL?

LTrim() and RTrim() Function in MS AccessIn LTrim() function a string will be pass as a parameter and it will return the string with no leading spaces. 2. RTrim() Function : It works same like LTrim() function but it will remove all trailing spaces from a string.

What does Ltrim do in SQL?

SQL Server LTRIM() Function The LTRIM() function removes leading spaces from a string.


1 Answers

MS SQL does not have a trim function. You'll need to use rTrim and lTrim together.

update MyTable set Name = lTrim(rTrim(name)) 
like image 196
Barry Jordan Avatar answered Sep 19 '22 11:09

Barry Jordan