How do I convert from BLOB to TEXT in MySQL?

SqlMysqlBlobs

Sql Problem Overview


I have a whole lot of records where text has been stored in a blob in MySQL. For ease of handling I'd like to change the format in the database to TEXT... Any ideas how easily to make the change so as not to interrupt the data - I guess it will need to be encoded properly?

Sql Solutions


Solution 1 - Sql

That's unnecessary. Just use SELECT CONVERT(column USING utf8) FROM..... instead of just SELECT column FROM...

Solution 2 - Sql

Here's an example of a person who wants to convert a blob to char(1000) with UTF-8 encoding:

CAST(a.ar_options AS CHAR(10000) CHARACTER SET utf8)

This is his answer. There is probably much more you can read about CAST right here. I hope it helps some.

Solution 3 - Sql

I have had the same problem, and here is my solution:

  1. create new columns of type text in the table for each blob column
  2. convert all the blobs to text and save them in the new columns
  3. remove the blob columns
  4. rename the new columns to the names of the removed ones

> ALTER TABLE mytable > ADD COLUMN field1_new TEXT NOT NULL, > ADD COLUMN field2_new TEXT NOT NULL; >
> update mytable set > field1_new = CONVERT(field1 USING utf8), > field2_new = CONVERT(field2 USING utf8); >
> alter table mytable > drop column field1, > drop column field2; >
> alter table mytable > change column field1_new field1 text, > change column field2_new field2 text;

Solution 4 - Sql

If you are using MYSQL-WORKBENCH, then you can select blob column normally and right click on column and click open value in editor. refer screenshot:

screenshot

Solution 5 - Sql

You can do it very easily.

ALTER TABLE `table_name` CHANGE COLUMN `column_name` `column_name` LONGTEXT NULL DEFAULT NULL ;

The above query worked for me. I hope it helps you too.

Solution 6 - Sql

None of these answers worked for me. When converting to UTF8, when the encoder encounters a set of bytes it can't convert to UTF8 it will result in a ? substitution which results in data loss. You need to use UTF16:

SELECT
    blobfield,
    CONVERT(blobfield USING utf16),
    CONVERT(CONVERT(blobfield USING utf16), BINARY),
    CAST(blobfield  AS CHAR(10000) CHARACTER SET utf16),
    CAST(CAST(blobfield  AS CHAR(10000) CHARACTER SET utf16) AS BINARY)

You can inspect the binary values in MySQL Workbench. Right click on the field -> Open Value in Viewer-> Binary. When converted back to BINARY the binary values should be the same as the original.

Alternatively, you can just use base-64 which was made for this purpose:

SELECT
    blobfield,
    TO_BASE64(blobfield),
    FROM_BASE64(TO_BASE64(blobfield))

Solution 7 - Sql

phpMyAdmin screenshot Using phpMyAdmin you can also set the options to show BLOB content and show complete text.

Solution 8 - Sql

Or you can use this function:

DELIMITER $$

CREATE FUNCTION BLOB2TXT (blobfield VARCHAR(255)) RETURNS longtext
DETERMINISTIC
NO SQL
BEGIN
       RETURN CAST(blobfield AS CHAR(10000) CHARACTER SET utf8);
END
$$


DELIMITER ;

Solution 9 - Sql

SELECCT TO_BASE64(blobfield)  
FROM the Table

worked for me.

The CAST(blobfield AS CHAR(10000) CHARACTER SET utf8) and CAST(blobfield AS CHAR(10000) CHARACTER SET utf16) did not show me the text value I wanted to get.

Solution 10 - Sql

I had the same issue with my MariaDB records. It was solved (by my colleague) using

select
uncompress(blobfield)
from table

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGwilymView Question on Stackoverflow
Solution 1 - SqlYumaView Answer on Stackoverflow
Solution 2 - SqlÓlafur WaageView Answer on Stackoverflow
Solution 3 - SqlAspedView Answer on Stackoverflow
Solution 4 - SqlankitView Answer on Stackoverflow
Solution 5 - SqlJignesh MesvaniyaView Answer on Stackoverflow
Solution 6 - SqlDean OrView Answer on Stackoverflow
Solution 7 - SqlmarcosnView Answer on Stackoverflow
Solution 8 - SqlAdam Sánchez AyteView Answer on Stackoverflow
Solution 9 - SqlKapidisView Answer on Stackoverflow
Solution 10 - SqlTHOTHView Answer on Stackoverflow