views:

29

answers:

2

Hello, I am not a guru of databases, do most of the things through PHP, like data manipulation as well, even if it is an internal task. However, PHP is not being very useful in this case. I have a table with about 0.4 million records in it and it has like 16 columns, everything else works fine but I populated this table from a text file and that text file had most of the values in each column in double quotes such as "United States of America" "London" etc.

I want to remove those double quotes where ever I can find them through a single query or SQL script. I have tried PHP and its str_replace method but apparently the records are far too huge for it to continue applying this function on each record and then run 2 queries for each value. Can there be any SQL solution for it?

The database is MySQL, engine can be switched from MyISAM to InnoDB. I have seen this solution here: http://stackoverflow.com/questions/834912/sql-search-for-a-string-in-every-varchar-column-in-a-database but not sure if it will work for me. Thanks.

+2  A: 

(untested)

update mytable set mycol = case
    when mycol like '"%"' then trim(both '"' from mycol)
    else mycol end;

Actually, you probably don't even need the case expression:

update mytable set mycol = trim(both '"' from mycol)'

Repeat the query for other columns, or extend this one to cover all of your columns.

Ned Batchelder
Is there any specific reason why you check for the '"%"' in a `case when` block instead of in the `where` section of the `update` query? Since neither will be using indexes it probably won't be faster, but it seems clearer to me to simply do `WHERE mycol LIKE '"%"'`
WoLpH
TRIM() remove only leading and trailing "remstr". I believe he wanted to remove them all.
Anpher
@WoLpH: you're probably right, that would be better.
Ned Batchelder
@Anpher: I assumed it was only quotes on the ends. In fact, if there are quotes internally, they're likely to be doubled or escaped, which makes it yet more complicated!
Ned Batchelder
@Ned batchelder: not much more difficult I think. You can simply do this to fix it: `replace(trim(both '"' from mycol), '""', '"')`
WoLpH
A: 

REPLACE() should work fine.

UPDATE `table` SET `column` = REPLACE(`column`, '"', '')
Anpher
That will replace every `"`. Although this is likely to be desired, I wouldn't say it's a guarantee.
WoLpH