mysql字段中提取汉字,去除数字以及字母

如果只是删除尾部的中文,保留数据,可以用以下的简单方式

MySQL [test]> select '1234万美元' + 0 as num;
+------+
| num |
+------+
| 1234 |
+------+
DELIMITER $$
DROP FUNCTION IF EXISTS `Num_char_extract`$$
CREATE FUNCTION `Num_char_extract`(Varstring VARCHAR(100)CHARSET utf8, flag INT) RETURNS VARCHAR(50) CHARSET utf8
COMMENT '标识 0 提取数字 1 提取字母 2提取数字+字母 3 提取汉字'
BEGIN
DECLARE len INT DEFAULT 0;
DECLARE Tmp VARCHAR(100) DEFAULT '';
SET len=CHAR_LENGTH(Varstring);
IF flag = 0
THEN
WHILE len > 0 DO
IF MID(Varstring,len,1)REGEXP'[0-9]' THEN
SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
END IF;
SET len = len - 1;
END WHILE;
ELSEIF flag=1
THEN
WHILE len > 0 DO
IF (MID(Varstring,len,1)REGEXP '[a-zA-Z]')
THEN
SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
END IF;
SET len = len - 1;
END WHILE;
ELSEIF flag=2
THEN
WHILE len > 0 DO
IF ( (MID(Varstring,len,1)REGEXP'[0-9]')
OR (MID(Varstring,len,1)REGEXP '[a-zA-Z]') )
THEN
SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
END IF;
SET len = len - 1;
END WHILE;
ELSEIF flag=3
THEN
WHILE len > 0 DO
IF NOT (MID(Varstring,len,1)REGEXP '^[u0391-uFFE5]')
THEN
SET Tmp=CONCAT(Tmp,MID(Varstring,len,1));
END IF;
SET len = len - 1;
END WHILE;
ELSE
SET Tmp = 'Error: The second paramter should be in (0,1,2,3)';
RETURN Tmp;
END IF;
RETURN REVERSE(Tmp);
END$$
DELIMITER ; -- select Num_char_extract('字12段',0);
上一篇:Lattice Diamond安装


下一篇:黄聪:wordpress向mysql字段中保存html代码(使用add_option()方法),然后无法显示出问题