Problems obtaining data when converting an int to a decimal field.


I have a table with two fields, nombre and cantidad de productos , I'm generating an additional field to see the amount of products consumed in 10 months so I'm doing a select like this:

select nombre, existencia , cast((cantidad/ 10) as decimal(6,2)) from productos 

and also ...

select nombre, existencia , convert(decimal (6,2), (cantidad/ 10)) decimal from productos

The problem is that it gives me the whole data and not with decimals, for example like this:

175 / 10 : 17.0 instead of showing it this way 17.5 , where 175 is cantidad . What is missing me?

asked by Fernando M. Goycochea 21.06.2018 в 23:15

1 answer


This happens because the conversion to decimal should be done before of the division.

Currently what is happening is that you have a% int , which you divide by 10, and that results in a entire division (without decimals), and then you convert it to decimal .

SELECT nombre, existencia , cast(cantidad as decimal(6,2))/10
from productos 
answered by 21.06.2018 / 23:19