简体   繁体   English

如何从 SQL 中的列中提取数值

[英]How to extract numeric values from a column in SQL

I am trying to extract only the numeric values from a column that contains cells that are exclusively numbers, and cells that are exclusively letter values, so that I can multiply the column with another that contains only numeric values.我试图从包含完全是数字的单元格和完全是字母值的单元格的列中提取数值,以便我可以将该列与仅包含数值的另一列相乘。 I have tried我努力了

SELECT trim(INTENT_VOLUME) 
from A
WHERE ISNUMERIC(INTENTVOLUME) 

and also并且

SELECT trim(INTENT_VOLUME) 
from A
WHERE ISNUMERIC(INTENTVOLUME) = 1

and neither works.两者都不起作用。 I get the error Function ISNUMERIC(VARCHAR) does not exist.我收到错误 Function ISNUMERIC(VARCHAR)不存在。 Can someone advise?有人可以建议吗? Thank you!谢谢!

It highly depends on DBMS.它高度依赖于 DBMS。

in SqlServer you have a limited built-in features to do it, so the next query may not work with all variants of your data:在 SqlServer 中,您的内置功能有限,因此下一个查询可能不适用于您的数据的所有变体:

select CAST(INTENT_VOLUME AS DECIMAL(10, 4)) 
from A 
where INTENT_VOLUME LIKE '%[0-9.-]%'
and INTENT_VOLUME NOT LIKE '%[^0-9.-]%';

In Oracle you can use regex in a normal way:在 Oracle 中,您可以正常使用正则表达式:

select to_number(INTENT_VOLUME) 
from A 
where REGEXP_LIKE(INTENT_VOLUME,'^[-+]?[0-9]+(\.[0-9]+)?$');

MySQL DBMS has also built-in regex MySQL DBMS 也内置了正则表达式

Try this, which tests if that text value can be cast as numeric...试试这个,它测试该文本值是否可以转换为数字......

select intent_volume from a where (intent_volume ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$') = 't' select intent_volume from a where (intent_volume ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$') = 't'

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

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