Is there an sql statement that will allow me to trim all leading and trailing spaces of all values in a table column?

Cheers,

Iain

PhilipToop
—
2010-01-22T21:51:03Z —
#2

UPDATE table set colname = TRIM(colname);

disgracian
—
2010-01-22T23:40:30Z —
#3

What database are you using?

Cheers,D.

Ken_Masters
—
2010-01-23T19:45:28Z —
#4

I'm using mysql 5.

Seems... UPDATE table set colname = TRIM(colname);

Isn't the right syntax. I'm doing the sql statement in phpmyadmin.

SJH
—
2010-01-23T20:20:04Z —
#5

That is the correct syntax for MySQL. What error did it give you?

PhilipToop
—
2010-01-23T20:50:23Z —
#6

Update tablename set colname = TRIM(colname);

is the right syntax.

colname is the name of the column whose values you wish to remove leading and trailing spaces from and tablename is the name of the table in which the column resides.

Ken_Masters
—
2010-01-23T20:58:29Z —
#7

Thanks Philip, I must have written it incorrectly but it works now... although no rows were affected, which is strange because I can see leading spaces in the majority of my rows in that column. I'm not sure how to get rid of them besides manually removing them.

SJH
—
2010-01-23T21:31:06Z —
#8

I had a similar problem in the past - read the thread I posted for the solution

disgracian
—
2010-01-24T01:04:05Z —
#9

Ken_Masters said:

I'm using mysql 5.

Then why didn't you post this question in the MySQL section?

Mods, can we have this moved?

Cheers,D.

PhilipToop
—
2010-01-24T10:54:21Z —
#10

You could use the following

SELECT * from tablename where colname like ' %' or colname like '% ';

to see if any columns with leading or trailing spaces are reported.

I am wondering if the character is not a space but is being seen as a space.