MySQL convert tables from latin1 to UTF-8

by Andrei Eftimie

I had a mysql dump with european characters encoded as Latin1 that needed to be imported into a new system.
Since its 2011 I hope we’re all using UTF-8 by now (if you are not, I will haunt you at night).

The problem

Even if your new DB is set to UTF-8, your legacy data might look like this:

Gøy og Lære på Rønningen

(mind you, this is only Norwegian)

Solution

Thanks to @mattiasgunneras article here http://www.bothernomore.com/2008/12/16/character-encoding-hell/ I managed to do the conversion very elegantly.

You just need to convert your column to a blob and then back to its original state. MySQL will handle the conversion for you. #win #tigerblood :


alter table TABLE_NAME modify FIELD_NAME blob;
alter table TABLE_NAME modify FIELD_NAME varchar(255) character set utf8;

Smooth UTF-8

Here’s the string once its been through the conversion process:

Gøy og Lære på Rønningen