Skip to content

Instantly share code, notes, and snippets.

@joni
Created June 19, 2012 19:35
Show Gist options
  • Save joni/2956080 to your computer and use it in GitHub Desktop.
Save joni/2956080 to your computer and use it in GitHub Desktop.
STRINGDECODE: MySQL function to decode unicode escapes to utf8
CREATE FUNCTION STRINGDECODE(str TEXT CHARSET utf8)
RETURNS text CHARSET utf8 DETERMINISTIC
BEGIN
declare pos int;
declare escape char(6) charset utf8;
declare unescape char(3) charset utf8;
set pos = locate('\\u', str);
while pos > 0 do
set escape = substring(str, pos, 6);
set unescape = char(conv(substring(escape,3),16,10) using ucs2);
set str = replace(str, escape, unescape);
set pos = locate('\\u', str, pos+1);
end while;
return str;
END
@AnthraX1
Copy link

shouldn't the second substring() at line 10 be substring(escape,3,4) ?

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