简体   繁体   中英

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. Can someone advise? Thank you!

It highly depends on 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:

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:

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

MySQL DBMS has also built-in regex

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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