Add in MYSQL query in varchar field by removing Strings

0

asked by anonymous 19.09.2018 / 21:23

2 answers

2

You need to add the converted value:

  • remove the R $,
  • remove point from thousands
  • Replace the comma by period

After that convert to number and add:

SELECT SUM(
cast(
  REPLACE(
    REPLACE(
       REPLACE(valor_total, 'R$ ', '') /* REMOVE R$*/
      , '.',''),   /* REMOVE O PONTO */
     ',','.')      /* TROCA A VÍRGULA POR PONTO */
        as decimal(8,2))
)  AS TotalDaSoma
FROM carrinho 
WHERE representante_id='92'

I broke into lines to make it easier to understand.

Here's a fiddle showing the conversion working: link

    
19.09.2018 / 21:47
1

It is necessary to chain the functions, leaving the one that must be executed first within the one that should be executed last, in your case it would look something like this:

SELECT SUM(REPLACE(REPLACE(REPLACE(REPLACE(valor_total, '.', ''), ',', '.'), ' ', ''), 'R$', '')) AS TotalDaSoma
FROM carrinho
WHERE representante_id='92'

In this way the following functions will be performed:

  • Replace . with nada ;
  • Replace , with . ;
  • Replace espaço with nada ;
  • Replace R$ with nada ;

Click here to see an example working.

    
19.09.2018 / 21:36