mysql 是否有像 is_num() 这样的函数可以让我确定数据是否为数字?
问问题
23110 次
6 回答
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 论坛 :: Microsoft SQL Server :: MySQL 中的 IsNumeric() 子句?
真实测试:
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)
虚假测试:
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)
于 2010-05-03T08:06:48.793 回答
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 |
+--------+
于 2010-05-15T18:15:21.520 回答
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;
于 2013-03-07T19:26:20.503 回答
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”
(顺便说一句,我在这里写了一篇关于这个的帖子)
于 2011-01-29T23:46:05.873 回答
1
IsNumeric 从 SQL 到 MYSQL 的简单语法是
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;
于 2016-08-18T09:27:39.153 回答
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 ;
于 2018-02-09T13:00:47.130 回答