简体   繁体   English

sql仅从列中提取数值

[英]sql extract only numeric value from column

I have a column called' memo_line_2',the value format is like :'$3000.00 (card limit increase)',how can I only extract numeric value from the column?Thanks example:我有一个名为“memo_line_2”的列,值格式类似于:“$3000.00(卡限额增加)”,我如何仅从该列中提取数值?谢谢示例:

'$3000.00 (card limit increase)' -> 3000
'$5000.00 (card limit increase)' -> 5000
'$12000.00 (card limit increase)' ->12000

You could use REGEXP_SUBSTR() for this:您可以REGEXP_SUBSTR()使用REGEXP_SUBSTR()

SELECT REGEXP_SUBSTR(tmp.`value`, '[0-9]+') as `new_value`
  FROM (SELECT '$3000.00' as `value` UNION ALL 
        SELECT '$5000.00' as `value` UNION ALL 
        SELECT '$12000.00' as `value`) tmp 

Returns:返回:

new_value
---------
3000
5000
12000

If you would like to keep everything after the decimal, use '[0-9.]+' as your regular expression filter.如果您想保留小数点后的所有内容,请使用'[0-9.]+'作为正则表达式过滤器。

If your data will be always in this format you can use below query to select the data between $ and .如果您的数据将始终采用这种格式,您可以使用以下查询来选择$和 之间的数据. :

SELECT substring_index(substring_index(memo_line_2, '$', -1), '.', 1)
FROM your_table;

Refrence: MySQL substring between two strings参考: 两个字符串之间的 MySQL 子字符串

You can use:您可以使用:

select regexp_substr(col, '[0-9]+[.]?[0-9]*')

This will extract the digits with the cents.这将提取带有美分的数字。 You can then convert to an integer or numeric:然后,您可以转换为整数或数字:

select cast(regexp_substr(col, '[0-9]+[.]?[0-9]*') as unsigned)

It can be done by making a custom function in your sql query可以通过在 sql 查询中创建自定义函数来完成

Take a look at:看一眼:

https://stackoverflow.com/a/37269038/16536522 https://stackoverflow.com/a/372​​69038/16536522

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM