1. 程式人生 > >MySQL:字串中的數字、英文字元、漢字提取

MySQL:字串中的數字、英文字元、漢字提取

原文:https://blog.csdn.net/oyezhou/article/details/81665643
另外一篇類似的:https://blog.csdn.net/haijiege/article/details/79460236

建立一個Num_char_extract.sql檔案,內容如下,在mysql shell中使用source執行,這樣就新建了一個函式。

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 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 ;
mysql> source /tmp/tmpcode/Num_char_extract.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.15 sec)

測試效果

mysql> select Num_char_extract("123中國ABC",0);
+------------------------------------+
| Num_char_extract("123中國ABC",0)   |
+------------------------------------+
| 123                                |
+------------------------------------+
1 row in set (0.06 sec)

mysql> select Num_char_extract("123中國ABC",2);
+------------------------------------+
| Num_char_extract("123中國ABC",2)   |
+------------------------------------+
| 123ABC                             |
+------------------------------------+
1 row in set (0.00 sec)

mysql> select Num_char_extract("123中國ABC",3);
+------------------------------------+
| Num_char_extract("123中國ABC",3)   |
+------------------------------------+
| 中國                               |
+------------------------------------+
1 row in set (0.00 sec)

mysql> insert into t2(name) values("1500萬人民幣元"),("300美元"),("54648萬人民幣"),("855598萬日元");
Query OK, 4 rows affected (0.14 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select name from t2;
+---------------------+
| name                |
+---------------------+
| 1500萬人民幣元      |
| 300美元             |
| 54648萬人民幣       |
| 855598萬日元        |
+---------------------+
4 rows in set (0.00 sec)

mysql> select name,Num_char_extract(name,3) from t2;
+---------------------+--------------------------+
| name                | Num_char_extract(name,3) |
+---------------------+--------------------------+
| 1500萬人民幣元      | 萬人民幣元               |
| 300美元             | 美元                     |
| 54648萬人民幣       | 萬人民幣                 |
| 855598萬日元        | 萬日元                   |
+---------------------+--------------------------+
4 rows in set (0.00 sec)