Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query - How do I convert a text field containing dollar sign to decimal only (strip out dollar sign)

I am running a query on a MySQl database and I have a field of text characters that contains a dollar amount with a dollar sign and a number (ie $1,345.89) I need to do a check for not NULL (some of the text fields are NULL) and that the value is greater than 0. How can I convert this text field to a decimal value so that I can verify that it is greater than 0. I do not want to alter the original text in the database.

Thanks

Here is a query I tried but it does not work:

SELECT parcel, year, due FROMdeliquent_property WHERE year IN ('2013', '2012', '2011', '2010') GROUP BY parcel HAVING due LIKE '%[0-9]%';

like image 561
Charles Bunn Avatar asked Dec 05 '25 10:12

Charles Bunn


1 Answers

This query will do the trick :

SELECT ID, 
       CAST(REPLACE(REPLACE(IFNULL(Amount,0),',',''),'$','') AS DECIMAL(10,2)) 
FROM Table1
WHERE CAST(REPLACE(REPLACE(IFNULL(Amount,0),',',''),'$','') AS DECIMAL(10,2)) > 0

See SQLFIDDLE : http://www.sqlfiddle.com/#!2/55f433/1/0

like image 62
Fabien TheSolution Avatar answered Dec 07 '25 16:12

Fabien TheSolution



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!