mysql是否有像is_num()这样的函数可以让我确定数据是否为数字?
答案 0 :(得分:6)
您可能想要创建一个用户定义的函数,该函数将值与正则表达式匹配:
CREATE FUNCTION IsNumeric (sIn varchar(1024)) RETURNS tinyint
RETURN sIn REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
来源:MySQL Forums :: Microsoft SQL Server :: IsNumeric() clause in MySQL?
Truthy测试:
mysql> SELECT ISNUMERIC('1');
+----------------+
| ISNUMERIC('1') |
+----------------+
| 1 |
+----------------+
1 row in set (0.01 sec)
mysql> SELECT ISNUMERIC(25);
+---------------+
| ISNUMERIC(25) |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql> SELECT ISNUMERIC('-100');
+-----------------+
| ISNUMERIC(-100) |
+-----------------+
| 1 |
+-----------------+
1 row in set (0.03 sec)
mysql> SELECT ISNUMERIC('1.5');
+------------------+
| ISNUMERIC('1.5') |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT ISNUMERIC('-1.5');
+-------------------+
| ISNUMERIC('-1.5') |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.02 sec)
Falsy Tests:
mysql> SELECT ISNUMERIC('a');
+----------------+
| ISNUMERIC('a') |
+----------------+
| 0 |
+----------------+
1 row in set (0.02 sec)
mysql> SELECT ISNUMERIC('a1');
+-----------------+
| ISNUMERIC('a1') |
+-----------------+
| 0 |
+-----------------+
1 row in set (0.00 sec)
mysql> SELECT ISNUMERIC('10a');
+------------------+
| ISNUMERIC('10a') |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT ISNUMERIC('0.a');
+------------------+
| ISNUMERIC('0.a') |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT ISNUMERIC('a.0');
+------------------+
| ISNUMERIC('a.0') |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
答案 1 :(得分:5)
在我的情况下,我只需要检查数据是否是> = 0而不是char,也许这对你也很有用:
mysql> select '1' REGEXP '^[0-9]+$' as result;
+--------+
| result |
+--------+
| 1 |
+--------+
1 row in set (0.16 sec)
mysql> select '1a' REGEXP '^[0-9]+$' as result;
+--------+
| result |
+--------+
| 0 |
+--------+
答案 2 :(得分:1)
如果您只想从字符串中选择数字字符,请尝试使用:
CREATE FUNCTION IsNumeric (val varchar(255)) RETURNS tinyint
RETURN val REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';
CREATE FUNCTION NumericOnly (val VARCHAR(255))
RETURNS VARCHAR(255)
BEGIN
DECLARE idx INT DEFAULT 0;
IF ISNULL(val) THEN RETURN NULL; END IF;
IF LENGTH(val) = 0 THEN RETURN ""; END IF;
SET idx = LENGTH(val);
WHILE idx > 0 DO
IF IsNumeric(SUBSTRING(val,idx,1)) = 0 THEN
SET val = REPLACE(val,SUBSTRING(val,idx,1),"");
SET idx = LENGTH(val)+1;
END IF;
SET idx = idx - 1;
END WHILE;
RETURN val;
END;
通过调用NumericOnly函数来使用它:
select NumericOnly('1&2') as result;
返回:“12”
select NumericOnly('abc987') as result;
返回:“987”
(顺便说一句,我写了post about this here)
答案 3 :(得分:1)
只需使用DECIMAL或SIGNED / UNSIGNED将数据显式转换为数字,然后使用STRCMP与原始数据值进行比较。如果STRCMP返回0(相等),那么您知道您有一个数字。否则,它不是。
以下是一个示例,假设数据中的数字没有小数位,因此我们可以将其转换为DECIMAL。
此查询选择is_it_a_number字段中包含数字数据的所有行。
SELECT * FROM my_table WHERE
STRCMP(CAST(my_table.is_it_a_number AS DECIMAL(10,0)), my_table.is_it_a_number) = 0;
答案 4 :(得分:0)
从SQL到MYSQL的IsNumeric的简单语法是
SQL:
SELECT isNumeric(col_name) FROM Table where isNumeric(col_name) <> 0
Mysql的
select concat('',col_name * 1) from table where concat('',col_name * 1) <> 0;
答案 5 :(得分:0)
快了一千倍。
DROP FUNCTION IF EXISTS NumericOnly;
DELIMITER $$
CREATE FUNCTION NumericOnly(val VARCHAR(25)) RETURNS VARCHAR(25)
BEGIN
DECLARE idx INT DEFAULT 0;
IF ISNULL(val) THEN RETURN NULL; END IF;
IF LENGTH(val) = 0 THEN RETURN ""; END IF;
SET idx = LENGTH(val);
WHILE idx > 0 DO
IF (SUBSTRING(val,idx,1) in ('0','1','2','3','4','5','6','7','8','9')) = 0 THEN
SET val = CONCAT(SUBSTRING(val, 1, idx-1), SUBSTRING(val FROM idx + 1));
END IF;
SET idx = idx - 1;
END WHILE;
RETURN val;
END;
$$
DELIMITER ;