BEGIN # by zouzp / 21-03-26 # unicode 轉中文字符的方法; # 注意 \ u 之間不能有空格,再傳入的字符串的時候不能有空格,和oracle 一樣會導致報錯提示傳入的數據有異常; DECLARE code1,code2,code3 VARCHAR(2000); -- 定義三個變量用於接收 unicode 字符串前面 和中間 和后面的字符 DECLARE s_index SMALLINT UNSIGNED DEFAULT 0; -- 定義用於定位index的變量 DECLARE result,tmp_txt TEXT; -- 定義返回用的變量; DECLARE temp VARCHAR(1); -- 臨時用的變量; SET s_index=LOCATE("\u", content,1); -- 獲取第一次出現'\u'字符的位置; SET result = ""; set tmp_txt = content; WHILE s_index>0 DO -- 獲取出現問題的字符的前面字符 set code1 = substr(tmp_txt,1,s_index-1); -- 獲取出問題的字段; SET code2 = substr(tmp_txt,s_index+1,4); -- 獲取后面未處理的字段 SET code3 = substr(tmp_txt,s_index+5); -- 修改有問題的字段 SET temp = CONVERT( unhex(code2) USING ucs2); -- 拼接字段 set tmp_txt = CONCAT(code1,temp,code3); SET s_index = LOCATE("\u", tmp_txt, 1);-- 再次查看有沒有保護\u 的unicode 的字符串; END WHILE ; SET result = tmp_txt; -- 返回; RETURN result; END