mysql 单汉字获取大写首拼(自定义函数)
描述: 字段值“北京天安门”,获取首个汉字“北”的首拼音大写“B”
函数: 创建自定义函数 field_chineseToFirstPinyin
案例: 自定义函数创建脚本
DROP FUNCTION IF EXISTS 自定义数据库.field_chineseToFirstPinyin;
create function 自定义数据库.field_chineseToFirstPinyin(a varchar(2000))
returns varchar(10)
deterministic
begin
declare result varchar(10);
set result = ELT(INTERVAL(CONV(HEX(left(CONVERT(a USING gbk),1)),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),
'A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','W','X','Y','Z');
return result;
end;
*执行结果
注: 该方案不能转阿拉伯数字和英文