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 ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful thanks!