Obtain the first letter of each Chinese character field in mysql

Source: Internet
Author: User

Set global log_bin_trust_function_creators = 1; SET FOREIGN_KEY_CHECKS = 0; -- retrieve Function structure for getPY -- revoke drop function if exists 'getpy'; DELIMITER ;; create definer = 'root' @ '%' FUNCTION 'getpy' (in_string VARCHAR (65534) RETURNS mediumtext CHARSET utf8BEGINDECLARE tmp_str VARCHAR (65534) charset gbk DEFAULT ''; # truncate the string, each cut The obtained string is stored in this variable. The initial value is the in_string value of the function parameter DECLARE tmp_len smallint default 0; # The length of tmp_str DECLARE tmp_char VARCHAR (2) charset gbk DEFAULT ''; # truncate character. The return value of left (tmp_str, 1) is stored in the variable DECLARE tmp_rs VARCHAR (65534) charset gbk DEFAULT ''; # result string DECLARE tmp_cc VARCHAR (2) charset gbk DEFAULT ''; # pinyin character, which stores the first character SET tmp_str = in_string; # initialize and assign in_string to tmp_strSET tmp_len = LENGTH (tmp_str ); # initialization length WHILE tmp_len> 0 DO # If If the length of the calculated tmp_str is greater than 0, enter the whileSET tmp_char = LEFT (tmp_str, 1); # obtain the first character at the leftmost end of tmp_str. Note that the first character is obtained here, this character may be a Chinese character or not. SET tmp_cc = tmp_char; # assign the first character on the left to the pinyin character if length (tmp_char)> 1 THEN # determine whether the first character on the left is multi-byte or single-byte, if it is multi-byte, it is considered as a Chinese character and obtained in the following pinyin format. If it is single-byte, It is not processed. Select elt (INTERVAL (CONV (HEX (tmp_char), 16,10), 0xB0A1, 0xB0C5, 0xB2C1, 0xB4EE, 0xB6EA, 0xB7A2, 0xB8C1, primary, 0xBBF7, primary, 0xC0AC, primary, 0xC4C3, 0xC5B6, 0xC5BE, 0xC6DA, 0xC8BB, 0xC8F6, 0 xCBFA, 0 xCDDA, 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; # Get the first character of Chinese pinyin end if; SET tmp_rs = CONCAT (tmp_rs, tmp_cc ); # concatenate the first character of the left end of the current tmp_str pinyin string with the return string SET tmp_str = SUBSTRING (tmp_str, 2 ); # Remove the first character from the left END of tmp_str from SET tmp_len = LENGTH (tmp_str); # Calculate the LENGTH of the current string end while; RETURN tmp_rs; # RETURN the string 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.