简体   繁体   中英

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:

'$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:

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.

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

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

Take a look at:

https://stackoverflow.com/a/37269038/16536522

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