Friday, November 25, 2011

Removing Chinese or non ASCII Characters from the end of a string in MySQL

Step 1. First make sure the string has Chinese or non ASCII characters in it:

select asciiname from table1
where asciiname regexp(concat('[',char(128),'-',char(255),']'));


Step 2. If the Chinese name is definitely at the end of your field, you just need to trim the last occurrence of a space in your field. Confirm it is doing the right thing first:

select asciiname, reverse(substring(reverse(asciiname), locate(' ', reverse( asciiname ) )+1))
from table1
where asciiname regexp(concat('[',char(128),'-',char(255),']'));


Step 3. Execute:

update table1
set asciiname = reverse(substring(reverse(asciiname), locate(' ', reverse( asciiname ) )+1))
where asciiname regexp(concat('[',char(128),'-',char(255),']'));




My sources for this were (I am very grateful to):
1. http://stackoverflow.com/questions/461871/find-rows-with-non-ascii-values-in-a-column

2. http://dev.mysql.com/doc/refman/5.1/en/string-functions.html



1 comment:

  1. Hello

    I really appreciate this type of information about MySQL. Because I have faced this problem. I was worries how can I solve this chines language error in My SQL. But Now I am going to use this method.

    James Williams
    Registry Booster 2012

    ReplyDelete