How to get the first letter of Chinese characters _mysql in MySQL

Source: Internet
Author: User
DROP FUNCTION IF EXISTS ' getpy '; 
DELIMITER;; CREATE FUNCTION ' getpy ' (in_string VARCHAR (65534)) RETURNS mediumtext CHARSET UTF8 BEGIN DECLARE tmp_str VARCHAR (65534) c Harset GBK DEFAULT '; #截取字符串, each time the intercepted string is stored in the variable, initially the function parameter in_string value DECLARE tmp_len SMALLINT default 0; #tmp_str的长度 DECLARE tmp_char VARCHAR (2) C Harset gbk default '; #截取字符, each left (tmp_str,1) return value is stored in the variable DECLARE tmp_rs VARCHAR (65534) charset gbk default '; #结果字符串 DE  CLARE tmp_cc VARCHAR (2) CharSet GBK DEFAULT '; #拼音字符, storing the phonetic first character SET tmp_str = in_string of a single Chinese character, assigning in_string to Tmp_str set Tmp_len = LENGTH (TMP_STR); #初始化长度 while Tmp_len > 0 does #如果被计算的tmp_str长度大于0则进入该while SET Tmp_char = left (tmp_str,1); #获 
Take the first character in the leftmost tmp_str, note that this is the first character, which may or may not be the character. 
SET TMP_CC = Tmp_char #左端首个字符赋值给拼音字符 if LENGTH (Tmp_char) >1 then# determines whether the first character on the left is a multi-byte or single-byte character, and if multibyte is considered to be a Chinese character and given the following pinyin, it is not treated as a single byte. SELECT ELT (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), ' 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; 
Set tmp_rs = CONCAT (TMP_RS,TMP_CC); #将当前tmp_str左端首个字符拼音首字符与返回字符串拼接 Set tmp_str = SUBSTRING (tmp_str,2); #将tmp_str左端首字符去除 
SET Tmp_len = LENGTH (TMP_STR); #计算当前字符串长度 end While; 
Return to Tmp_rs #返回结果字符串 end;; DELIMITER;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.