Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert latin1_swedish_ci data into utf8_general_ci?

I have a MySQL database with all the table fields collation as

latin1_swedish_ci

It has almost 1000 of the records already stored and now I want to convert all these data into

utf8_general_ci

So that I can display any language content. I have already altered the field collations into utf8_general_ci but this does not CONVERT all the old records into utf8_general_ci

like image 932
aslamdoctor Avatar asked Oct 06 '12 05:10

aslamdoctor


People also ask

How do I change utf8mb4 to utf8?

To solve the problem open the exported SQL file, search and replace the utf8mb4 with utf8 , after that search and replace the utf8mb4_unicode_520_ci with utf8_general_ci . Save the file and import it into your database. After that, change the wp-config. php charset option to utf8 , and the magic starts.

What is latin1_swedish_ci in MySQL?

The default character set for MySQL at (mt) Media Temple is latin1, with a default collation of latin1_swedish_ci. This is a common type of encoding for Latin characters. You can also change the encoding. utf8 is a common character set for non-Latin characters.

How do I change a character set from latin1 to utf8 in MySQL?

Similarly, here's the command to change character set of MySQL table from latin1 to UTF8. Replace table_name with your database table name. mysql> ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; Hopefully, the above tutorial will help you change database character set to utf8mb4 (UTF-8).

What is latin1_swedish_ci?

latin1_swedish_ci is a single byte character set, unlike utf8_general_ci . Compared to latin1_general_ci it has support for a variety of extra characters used in European languages. So it's a best choice if you don't know what language you will be using, if you are constrained to use only single byte character sets.


1 Answers

one funny thing.

CONVERT TO CHARSET and CONVERT()/CAST() suggested by Anshu will work fine if charset in the table is in right encoding.

If for some reason latin1 column containts utf8 text, CONVERT() and CAST() will not be able to help. I had "messed" my database with that setup so spend bit more time on solving this.

to fix this in addition to character set conversion, there are several exercises required.

  1. "Hard one" is to recreate the database from dump that will be converted via console
  2. "Simple one" is to convert row by row or table by table:
INSERT INTO UTF8_TABLE (UTF8_FIELD)
SELECT convert(cast(convert(LATIN1_FIELD using latin1) as binary) using utf8)
  FROM LATIN1_TABLE;

basically, both cases will process string to original symbols and then to right encoding, that won't happen with simple convert(field using encoding) from table; command.

like image 125
E_ONE Avatar answered Sep 21 '22 02:09

E_ONE