Search code examples
phpmysqlutf-8latin1

MySQL function to convert a text field encoded with Latin1 to UTF8


I want to convert my current MySQL database encoded with latin1 to a new database encoded with utf-8.

How could I import my old data (text fields) correctly to the tables of the new database with utf-8 encoding? I thought I can use a query as follows:

insert into newTable (newField) select latin2utf8(oldField) 
from oldTable

What I am looking for is a SQL function or a combination of functions which works as latin2utf8 mentioned in the query.

Is there any SQL method for converting data as I described, to be used in this command?


Solution

  • In the query that is reading rows from the old database, first, convert the column to binary, then convert it back to utf8 as follows:

    select convert(binary convert(field_name using latin1) using utf8) 
    from table_name