Created
August 2, 2012 11:20
-
-
Save joni/3236366 to your computer and use it in GitHub Desktop.
maybe_utf8_decode: MySQL function to recover doubly UTF-8 encoded text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; |
This has been proved very useful! +1
Very useful thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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