MySql 漢字轉拼音首字母

2021-09-02 21:22:21 字數 1969 閱讀 9084

delimiter $$

use `test`$$

drop function if exists `getpy`$$

create definer=`hjd`@`%` function `getpy`(in_string varchar(21845)) returns varchar(21845) charset utf8

begin

#擷取字串,每次做擷取後的字串存放在該變數中,初始為函式引數in_string值

declare tmp_str varchar(21845) charset gbk default '' ;

#tmp_str的長度

declare tmp_len smallint default 0;

#tmp_str的長度

declare tmp_loc smallint default 0;

#擷取字元,每次 left(tmp_str,1) 返回值存放在該變數中

declare tmp_char varchar(2) charset gbk default '';

#結果字串

declare tmp_rs varchar(21845)charset gbk default '';

#拼音字元,存放單個漢字對應的拼音首字元

declare tmp_cc varchar(2) charset gbk default '';

#初始化,將in_string賦給tmp_str

set tmp_str = in_string;

#初始化長度

set tmp_len = length(tmp_str);

#如果被計算的tmp_str長度大於0則進入該while

while tmp_len > 0 do

#獲取tmp_str最左端的首個字元,注意這裡是獲取首個字元,該字元可能是漢字,也可能不是。

set tmp_char = left(tmp_str,1);

#左端首個字元賦值給拼音字元

set tmp_cc = tmp_char;

#獲取字元的編碼範圍的位置,為了確認漢字拼音首字母是那乙個

set tmp_loc=interval(conv(hex(tmp_char),16,10),0xb0a1,0xb0c5,0xb2c1,0xb4ee,0xb6ea,0xb7a2,0xb8c1,0xb9fe,0xbbf7,0xbfa6,0xc0ac

,0xc2e8,0xc4c3,0xc5b6,0xc5be,0xc6da,0xc8bb,0xc8f6,0xcbfa,0xcdda ,0xcef4,0xd1b9,0xd4d1);

#判斷左端首個字元是多位元組還是單位元組字元,要是多位元組則認為是漢字且作以下拼音獲取,要是單位元組則不處理。如果是多位元組字元但是不在對應的編碼範圍之內,即對應的不是大寫字母則也不做處理,這樣數字或者特殊字元就保持原樣了

if (length(tmp_char)>1 and tmp_loc>0 and tmp_loc<24) then

#獲得漢字拼音首字元

select elt(tmp_loc,'a','b','c','d','e','f','g','h','j','k','l','m','n','o','p','q','r','s','t','w','x','y','z') into tmp_cc;

end if;

#將當前tmp_str左端首個字元拼音首字元與返回字串拼接

set tmp_rs = concat(tmp_rs,tmp_cc);

#將tmp_str左端首字元去除

set tmp_str = substring(tmp_str,2);

#計算當前字串長度

set tmp_len = length(tmp_str);

end while;

#返回結果字串

return tmp_rs;

end$$

delimiter ;

漢字轉拼音首字母

public string hz2py string hz 獲得漢字的區位碼 else if tmp 45253 tmp 45760 else if tmp 47761 tmp 46317 else if tmp 46318 tmp 46825 else if tmp 46826 tmp 47009...

漢字轉拼音或轉拼音首字母

漢字轉拼音或轉拼音首字母 public class chinesetospell private static string pyname new string 把漢字轉換成拼音 全拼 漢字字串 轉換後的拼音 全拼 字串 public static string characterconvertst...

漢字轉拼音或轉拼音首字母

漢字轉拼音或轉拼音首字母 public class chinesetospell private static string pyname new string 把漢字轉換成拼音 全拼 漢字字串 轉換後的拼音 全拼 字串 public static string characterconvertst...