trimming extra spaces from large DB file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hi, I need some advice on what to do. I have this MySQL insert file,
with about 30,000 records. One of the datafields has names in it. When
this list was put together, apparently there was extra space formatting
inserted at the end of each name. I want to remove these, so for

INSERT INTO `table_one` VALUES ('John Doe     ', 1);
INSERT INTO `table_one` VALUES ('Jane Doe     ', 2);
INSERT INTO `table_one` VALUES ('Baby S. Doe        ', 3);

I want:

INSERT INTO `table_one` VALUES ('John Doe', 1);
INSERT INTO `table_one` VALUES ('Jane Doe', 2);
INSERT INTO `table_one` VALUES ('Baby S. Doe', 3);

I was thinking some kind of script could be put together to trim the
extra spaces out. I have this entire insert list as a text file and can
re-insert it, or leave it in the DB and run queries against it. What
complicates this for me is some of the name fields have middle names,
some do not. Maybe something to remove all spaces greater than one on a
first pass, and then on a second pass trim the extra space at the end
only. That is what I think of in theory - but have no idea how to
create such script. Can anyone help? I would be very much appreciated.



Re: trimming extra spaces from large DB file

MySQL has a trim function so I think the easiest way would be to load the
file into MySQL and then:

UPDATE table_one SET `name` = TRIM(`name`);

Replace `name` with whatever the column name is.

Best Regards,

  Peter Albertsson

Quoted text here. Click to load it

Site Timeline