Convert string to MYSQL integer

4

What command can I use to convert string to integer in mysql? for example:

I'm doing the following list of tables:

SELECT mov . * , prod.produto, prod.unidade, prod.icms, prod.ipi, prod.codigo
FROM movimentacao AS mov, produtos AS prod
WHERE mov.Codigo = prod.codigo 

The prod.codigo field has the value 0259 and the mov.Codigo field is with 259 and at the time of the comparison they are not equal, I was wondering if you have any command that converts the value of prod.codigo for integer to get 259

    
asked by anonymous 25.04.2014 / 17:35

3 answers

6

Use CAST() :

SELECT mov . * , prod.produto, prod.unidade, prod.icms, prod.ipi, cast(prod.codigo as unsigned integer)
FROM movimentacao AS mov, produtos AS prod
WHERE mov.Codigo = prod.codigo 

Source: link

    
25.04.2014 / 17:38
3

Just force a numeric operation on the string:

SELECT mov.* , prod.produto, prod.unidade, prod.icms, prod.ipi, cast(prod.codigo as unsigned integer)
FROM movimentacao AS mov, produtos AS prod
WHERE mov.Codigo = 0 + prod.codigo
    
25.04.2014 / 17:45
3

This way if you do not have any typos

Function: Cast

SELECT mov.* , 
       prod.produto, 
       prod.unidade, 
       prod.icms, 
       prod.ipi, 
       prod.prodcodigo
FROM movimentacao AS mov, 
     (
        SELECT CAST(prod.codigo) as prodcodigo,
            prod.produto, 
            prod.unidade, 
            prod.icms, 
            prod.ipi
     ) AS prod
WHERE mov.Codigo = prod.prodcodigo

Obs: prod.codigo was prod.prodcodigo to differentiate from mov.codigo

References:

25.04.2014 / 18:04