Skip to content

Instantly share code, notes, and snippets.

@joni
Created August 2, 2012 11:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joni/3236366 to your computer and use it in GitHub Desktop.
Save joni/3236366 to your computer and use it in GitHub Desktop.
maybe_utf8_decode: MySQL function to recover doubly UTF-8 encoded text
DELIMITER $$
CREATE FUNCTION maybe_utf8_decode(str text charset utf8)
RETURNS text CHARSET utf8 DETERMINISTIC
BEGIN
declare str_converted text charset utf8;
declare max_error_count int default @@max_error_count;
set @@max_error_count = 0;
set str_converted = convert(binary convert(str using latin1) using utf8);
set @@max_error_count = max_error_count;
if @@warning_count > 0 then
return str;
else
return str_converted;
end if;
END$$
DELIMITER ;
@vegai
Copy link

vegai commented Oct 10, 2013

Note that some mysql-5.0 versions have a bug that crashes the database when running this script. It has to do with the identical name max_error_count and @@max_error_count.

Can be circumvented by renaming max_error_count to e.g. _max_error_count, lines 7 and 10

@nicolapetracchi
Copy link

This has been proved very useful! +1

Copy link

ghost commented Jul 29, 2015

Very useful thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment