it-swarm.cn

如何检查MySQL中的值是否为整数?

我在MySQL中看到有Cast()Convert()函数从值创建整数,但有没有办法检查一个值是否为整数?像PHP中的is_int()之类的东西就是我要找的东西。

105
Craig Nakamoto

我假设您要检查字符串值。一个不错的方法是REGEXP运算符,将字符串与正则表达式匹配。简单地做

select field from table where field REGEXP '^-?[0-9]+$';

这个速度相当快。如果您的字段是数字,只需测试

ceil(field) = field

代替。

189
Jumpy

将其与正则表达式匹配。

c.F. http://forums.mysql.com/read.php?60,1907,38488#msg-38488 引用如下:

Re:MySQL中的IsNumeric()子句?
发布者:kevinclark()
日期:2005年8月8日01:01 PM


我同意。这是我为MySQL 5创建的一个函数:

CREATE FUNCTION IsNumeric (sIn varchar(1024)) RETURNS tinyint
RETURN sIn REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';


这允许在开头有一个可选的加号/减号,一个可选的小数点和其余的数字。

13
JBB

假设我们的字段包含字母数字字段,其中包含条目

a41q
1458
xwe8
1475
asde
9582
.
.
.
.
.
qe84

并且您希望此db列中的最高数值(在本例中为9582),那么此查询将帮助您

SELECT Max(column_name) from table_name where column_name REGEXP '^[0-9]+$'
10
Tarun Sood

假设数据类型为varchar,这是一个简单的解决方案

select * from calender where year > 0

如果年份为数字,则返回true,否则返回false

8
Jayjitraj

这也有效:

CAST( coulmn_value AS UNSIGNED ) // will return 0 if not numeric string.

例如

SELECT CAST('a123' AS UNSIGNED) // returns 0
SELECT CAST('123' AS UNSIGNED) // returns 123 i.e. > 0
6
Riad

要检查Mysql中的值是否为Int,我们可以使用以下查询。此查询将为行提供Int值

SELECT col1 FROM table WHERE concat('',col * 1) = col;
4
minhas23

关于什么:

WHERE table.field = "0" or CAST(table.field as SIGNED) != 0

测试数字和证据:

WHERE table.field != "0" and CAST(table.field as SIGNED) = 0
3
Tom Auger

我能想到的最好的变量是int是与MySQL的函数CAST()LENGTH()的组合。
此方法适用于字符串,整数,双精度/浮点数据类型。

SELECT (LENGTH(CAST(<data> AS UNSIGNED))) = (LENGTH(<data>)) AS is_int

见demo http://sqlfiddle.com/#!9/ff40cd/44

如果列具有单个字符值,则会失败。如果列的值为'A',则Cast('A'为UNSIGNED)将评估为0,LENGTH(0)将为1. so LENGTH(Cast('A'为UNSIGNED))= LENGTH(0)将评估为1 = 1 => 1

True Waqas Malik完全用于测试那个案子。补丁是。

SELECT <data>, (LENGTH(CAST(<data> AS UNSIGNED))) = CASE WHEN CAST(<data> AS UNSIGNED) = 0 THEN CAST(<data> AS UNSIGNED) ELSE (LENGTH(<data>)) END AS is_int;

结果

**Query #1**

    SELECT 1, (LENGTH(CAST(1 AS UNSIGNED))) = CASE WHEN CAST(1 AS UNSIGNED) = 0 THEN CAST(1 AS UNSIGNED) ELSE (LENGTH(1)) END AS is_int;

| 1   | is_int |
| --- | ------ |
| 1   | 1      |

---
**Query #2**

    SELECT 1.1, (LENGTH(CAST(1 AS UNSIGNED))) = CASE WHEN CAST(1.1 AS UNSIGNED) = 0 THEN CAST(1.1 AS UNSIGNED) ELSE (LENGTH(1.1)) END AS is_int;

| 1.1 | is_int |
| --- | ------ |
| 1.1 | 0      |

---
**Query #3**

    SELECT "1", (LENGTH(CAST("1" AS UNSIGNED))) = CASE WHEN CAST("1" AS UNSIGNED) = 0 THEN CAST("1" AS UNSIGNED) ELSE (LENGTH("1")) END AS is_int;

| 1   | is_int |
| --- | ------ |
| 1   | 1      |

---
**Query #4**

    SELECT "1.1", (LENGTH(CAST("1.1" AS UNSIGNED))) = CASE WHEN CAST("1.1" AS UNSIGNED) = 0 THEN CAST("1.1" AS UNSIGNED) ELSE (LENGTH("1.1")) END AS is_int;

| 1.1 | is_int |
| --- | ------ |
| 1.1 | 0      |

---
**Query #5**

    SELECT "1a", (LENGTH(CAST("1.1" AS UNSIGNED))) = CASE WHEN CAST("1a" AS UNSIGNED) = 0 THEN CAST("1a" AS UNSIGNED) ELSE (LENGTH("1a")) END AS is_int;

| 1a  | is_int |
| --- | ------ |
| 1a  | 0      |

---
**Query #6**

    SELECT "1.1a", (LENGTH(CAST("1.1a" AS UNSIGNED))) = CASE WHEN CAST("1.1a" AS UNSIGNED) = 0 THEN CAST("1.1a" AS UNSIGNED) ELSE (LENGTH("1.1a")) END AS is_int;

| 1.1a | is_int |
| ---- | ------ |
| 1.1a | 0      |

---
**Query #7**

    SELECT "a1", (LENGTH(CAST("1.1a" AS UNSIGNED))) = CASE WHEN CAST("a1" AS UNSIGNED) = 0 THEN CAST("a1" AS UNSIGNED) ELSE (LENGTH("a1")) END AS is_int;

| a1  | is_int |
| --- | ------ |
| a1  | 0      |

---
**Query #8**

    SELECT "a1.1", (LENGTH(CAST("a1.1" AS UNSIGNED))) = CASE WHEN CAST("a1.1" AS UNSIGNED) = 0 THEN CAST("a1.1" AS UNSIGNED) ELSE (LENGTH("a1.1")) END AS is_int;

| a1.1 | is_int |
| ---- | ------ |
| a1.1 | 0      |

---
**Query #9**

    SELECT "a", (LENGTH(CAST("a" AS UNSIGNED))) = CASE WHEN CAST("a" AS UNSIGNED) = 0 THEN CAST("a" AS UNSIGNED) ELSE (LENGTH("a")) END AS is_int;

| a   | is_int |
| --- | ------ |
| a   | 0      |

演示

2
Raymond Nijland

我已尝试使用上面列出的正则表达式,但它们不适用于以下内容:

SELECT '12 INCHES' REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$' FROM ...

以上将返回1TRUE),这意味着对上面的正则表达式测试字符串'12 INCHES',返回TRUE。它看起来像是基于上面使用的正则表达式的数字。在这种情况下,因为12位于字符串的开头,所以正则表达式将其解释为数字。

以下将返回正确的值(即0),因为字符串以字符而不是数字开头

SELECT 'TOP 10' REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$' FROM ...

以上将返回0FALSE),因为字符串的开头是文本而不是数字。

但是,如果您要处理的数字和字母混合的字符串以数字开头,则无法获得所需的结果。 REGEXP会将字符串解释为有效数字,而实际上并非如此。

2
Bill Kelly

这适用于VARCHAR,它以数字开头或不是。

WHERE concat('',fieldname * 1) != fieldname 

当你到达更大的NNNNE + - 数字时可能会受到限制

0
PodTech.io

对我来说,唯一有效的是:

CREATE FUNCTION IsNumeric (SIN VARCHAR(1024)) RETURNS TINYINT
RETURN SIN REGEXP '^(-|\\+){0,1}([0-9]+\\.[0-9]*|[0-9]*\\.[0-9]+|[0-9]+)$';

来自kevinclark所有其他返回无用的东西给我在234jk45612 inches的情况下

0
Tim