如何从mysql中只选择数字数据?

发布于 2024-08-31 02:28:55 字数 46 浏览 6 评论 0原文

mysql 是否有像 is_num() 这样的函数可以让我确定数据是否是数字?

Does mysql have a function like is_num() which can allow me determine the data is numeric or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

难理解 2024-09-07 02:28:55

您可能想要创建一个用户定义的函数,将值与正则表达式相匹配:

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)
;

来源:MySQL 论坛 :: Microsoft SQL Server :: MySQL 中的 IsNumeric() 子句?


真实测试:

虚假测试:

You may want to create a user defined function that matches the value against a regular expression:

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

Source: MySQL Forums :: Microsoft SQL Server :: IsNumeric() clause in MySQL?


Truthy Tests:

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)
;

Source: MySQL Forums :: Microsoft SQL Server :: IsNumeric() clause in MySQL?


Truthy Tests:

Falsy Tests:

谈情不如逗狗 2024-09-07 02:28:55

就我而言,我只需要检查数据是否 >= 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 | 
+--------+

In my case I just needed to check if data was >= 0 and not char, perhaps this is useful for you also:

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 | 
+--------+
冧九 2024-09-07 02:28:55

如果您只想从字符串中选择数字字符,请尝试使用此方法:

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

通过调用 NumericOnly 函数来使用它,如下所示:

select NumericOnly('1&2') as result;

返回:“12”

select NumericOnly('abc987') as result;

返回:“987”

(顺便说一句,我写了一个 在这里发布有关此内容

; 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 函数来使用它,如下所示:

返回:“12”

返回:“987”

(顺便说一句,我写了一个 在这里发布有关此内容

If you want to select only the numeric characters from your string, try using this:

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

Use it by calling the NumericOnly function like this:

select NumericOnly('1&2') as result;

Returns: "12"

select NumericOnly('abc987') as result;

Returns: "987"

(BTW, I wrote a post about this here)

; 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;

Use it by calling the NumericOnly function like this:

Returns: "12"

Returns: "987"

(BTW, I wrote a post about this here)

泪是无色的血 2024-09-07 02:28:55

只需使用 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;

Just convert your data explicitly to a number using DECIMAL or SIGNED/UNSIGNED, and use STRCMP to compare to the original data value. If STRCMP returns 0 (equality) then you know you have a number. Otherwise, it's not.

Here's an example that assumes there are no decimal places in your data when it's numeric, so we can cast as DECIMAL.

This query select all the rows that contain numeric data in the is_it_a_number field.

SELECT * FROM my_table WHERE
    STRCMP(CAST(my_table.is_it_a_number AS DECIMAL(10,0)), my_table.is_it_a_number) = 0;
晨光如昨 2024-09-07 02:28:55

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;

The simple syntax for IsNumeric from SQL to MYSQL is

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;
孤蝉 2024-09-07 02:28:55

速度快千倍。

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 ;

Thousand times faster.

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 ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文