MySQL自定义函数提取中文

 函数构造

1、变量varstring

2、计算变量varstring的长度

3、截取varstring并正则判断

4、将判断结果追加到变量v_temp

5、循环3-4

6、返回v_temp

SET @c='/BayerPharmaAG//BayerPharmaAG拜耳医药保健有限公司';


set global log_bin_trust_function_creators=TRUE;
-- 定义函数
DROP FUNCTION GetChinse;
CREATE FUNCTION GetChinse(varstring VARCHAR(255))
RETURNS VARCHAR(255)
BEGIN
DECLARE v_length INT DEFAULT 0;
DECLARE v_temp VARCHAR(255) DEFAULT '';

SET v_length=CHAR_LENGTH(varstring);
WHILE v_length>0 DO

	IF MID(varstring,v_length,1) REGEXP'[\\u4e00-\\u9fa5]' THEN
	SET v_temp=CONCAT(v_temp,MID(varstring,v_length,1));
-- ELSE
	-- statement_list
	END IF;

	SET v_length=v_length-1;
END WHILE;
RETURN REVERSE(v_temp);
END;

SELECT GetChinse(@c);

 

你可能感兴趣的:(MySQL自定义函数提取中文)