使用MySQL从一个字符串中返回一个浮点数 [英] Return a float number from a string using MySQL

查看:351
本文介绍了使用MySQL从一个字符串中返回一个浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些类似于表mutable的表,例如这样的数据:


$ b $ id size
1 100ml
2 30ml
3 6,7g
4 8,8kg
5 alfa 110v
6 beta 220v

如何提取一个字符串的数字并将其转换为一个浮点数。 (在这种情况下,将小数部分从整数中分离出来。

那么,如何执行查询返回这样的东西?:

 
id大小finalSize
3 6,7g 6,7
4 8 ,8kg 8,8
2 30ml 30
1 100ml 100
5 alfa 110v 110
6 beta 220v 220

和这个(只是id`s):

 
id
3
4:
2
1
5
6

PS:我需要这样做与MySQL函数...

我已经尝试搜索匹配一个正则表达式使用MySQL,但看起来像没有函数返回
也许如果我创建一个函数来替换不是数字或者的everthing,可以解决这个问题,但是我找不到MYSQL REPLACE函数不使用正则表达式。

解决方案

这是一个基于乐趣的解决方案ction:该功能已经可以在其中的一篇文章中找到我只是改变了添加一个逗号到列表中,因为它运作良好:)



查询:

  select id,size,
replace(STRIP_NON_DIGIT(size),' ,','。')
作为最后
来自demo
;

功能:

 < codeREATE FUNCTION STRIP_NON_DIGIT(input VARCHAR(255))
RETURNS VARCHAR(255)
BEGIN
DECLARE output VARCHAR(255)DEFAULT'';
DECLARE iterator INT DEFAULT 1;
WHILE迭代器< (输入,迭代器,1)IN(',','0','1','2','3','4','5 ','6','7','8','9')THEN
SET输出= CONCAT(输出,SUBSTRING(输入,迭代器,1));
END IF;
SET iterator = iterator + 1;
END WHILE;
RETURN输出;
END //

结果:

  | ID | SIZE | FINAL | 
--------------------------
| 3 | 6,7g | 6.7 |
| 4 | 8,8kg | 8.8 |
| 2 | 30ml | 30 |
| 1 | 100ml | 100 |
| 5 |阿尔法110v | 110 |
| 6 |测试版220v | 220 |

正如您自己提到的,MYSQL没有 regex_replace Oracle 。除非你想使用多个替换(不想假设多少),否则一个函数会派上用场。


If I have something like table called mutable, for example, with this kind of data:

id size
1  100ml
2  30ml
3  6,7g
4  8,8kg
5  alfa 110v
6  beta 220v

How can I extract the number of a string and convert it to a float number. (At this case the , separates the decimal part from the integer one.

So, how can I perform a query to return something like this?:

id size       finalSize
3  6,7g       6,7
4  8,8kg      8,8
2  30ml       30
1  100ml      100
5  alfa 110v  110
6  beta 220v  220

and this? (just the id`s):

id
3
4
2
1
5
6

P.S.: I need to do this with MySQL functions...

P.S.:2 I have tried search about match a regex using MySQL, but looks like there aren't functions to return the matched pattern. Maybe if I create a function that replaces everthing that isn't a digit or , can solve the problem to me, but I couldn't find a way to do this. MYSQL REPLACE function doesn't use regex.

解决方案

Here is a solution based on a function: The function was readily available in one of thes posts. So I only changed adding a comma into the list as it works well :)

Query:

select id, size,
  replace(STRIP_NON_DIGIT(size),',','.')
  as final
from demo
;

Function:

CREATE FUNCTION STRIP_NON_DIGIT(input VARCHAR(255))
   RETURNS VARCHAR(255)
BEGIN
   DECLARE output   VARCHAR(255) DEFAULT '';
   DECLARE iterator INT          DEFAULT 1;
   WHILE iterator < (LENGTH(input) + 1) DO
      IF SUBSTRING(input, iterator, 1) IN (',', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ) THEN
         SET output = CONCAT(output, SUBSTRING(input, iterator, 1));
      END IF;
      SET iterator = iterator + 1;
   END WHILE;   
   RETURN output;
END//

Results:

| ID |      SIZE | FINAL |
--------------------------
|  3 |      6,7g |   6.7 |
|  4 |     8,8kg |   8.8 |
|  2 |      30ml |    30 |
|  1 |     100ml |   100 |
|  5 | alfa 110v |   110 |
|  6 | beta 220v |   220 |

As you mentioned yourself, MYSQL doesn't have the regex_replace like Oracle. Unless you want to use multiple number of replaces (wouldn't want to assume how many), a funciton would come in handy for you.

这篇关于使用MySQL从一个字符串中返回一个浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆