CAST for monetary value stored in text in MySQL
In the Bank product table there is a field valor
that has the type varchar(255)
Note the query:
See
SELECT
valor as valor_original,
CAST(valor AS DECIMAL(18,2)) as valor_convertido
from
produtos
Result
valor_original | valor_convertido
170,00 170.00
204,80 204.00
447,95 447.00
170,00 170.00
209,00 209.00
230,40 230.00
139,00 139.00
209,00 209.00
315,00 315.00
230,40 230.00
170,00 170.00
How can I perform a query by performing the conversion in decimal
without losing the accuracy of the values ?
1
Author: stringnome, 2016-10-11
1 answers
I managed to solve using Replace along with CAST
See
SELECT
valor AS valor_original,
CAST(REPLACE(valor, ',', '.') AS DECIMAL(18,2)) as valor_convertido
from
produtos
Result
valor_original | valor_convertido
170,00 170.00
204,80 204.80
447,95 447.95
170,00 170.00
209,00 209.00
230,40 230.40
139,00 139.00
209,00 209.00
315,00 315.00
230,40 230.40
170,00 170.00
2
Author: stringnome, 2016-10-11 14:29:47