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

BlueChippy picture BlueChippy · Oct 18, 2011 · Viewed 62.6k times · Source

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?

Answer

Barry Jordan picture Barry Jordan · Oct 18, 2011

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

update MyTable set Name = lTrim(rTrim(name))