Remove special characters from a database field

前端 未结 10 1684
梦如初夏
梦如初夏 2020-12-06 01:00

I have a database with several thousand records, and I need to strip down one of the fields to ensure that it only contains certain characters (Alphanumeric, spaces, and sin

10条回答
  •  醉酒成梦
    2020-12-06 01:59

    Adeel's answer is by far the best and simplest.

    The OP needed to update the db, which is what I need too. So I figured I'd put that here for the next poor sole, like me, not to have to redo what I did.

    Double check first, select it and scan them to make sure you're getting the right rows, before you update.

    SELECT REGEXP_REPLACE(columnName, '[^\\x20-\\x7E]', '') from tableName;
    

    Count to do a safety check ...

    SELECT count(*) from tableName WHERE columnName REGEXP '[^\\x20-\\x7E]';
    

    For some names I had to do another mapping so as not to lose their meaning like Ramon to Ramn because the o has a umlaut or grave or circumflex. So I used this to map ... https://theasciicode.com.ar

    Then update This update is a catch all after the mapping update. Change the limit number to the count value above ...

    UPDATE tablename SET columnName = REGEXP_REPLACE(columnName, '[^\\x20-\\x7E]', '') WHERE columnName REGEXP '[^\\x20-\\x7E]' LIMIT 1;
    

提交回复
热议问题