select 19/12
return 1,I need in decimal that is 1.58
I am using sqlserver 2005
select 19/12
return 1,I need in decimal that is 1.58
I am using sqlserver 2005
You have to do a decimal or float conversion before the CAST. Otherwise it is just integer division
SELECT
CAST(1.00 * 19 / 12 AS DECIMAL(19,2))
However, the 2 decimal place thing is a presentation issue. I'd do that in the client code
I'm a MySQL fan, but you could try this:
Maybe SELECT 19.0/12.0
Will give you the answer you're looking for?
There are a couple of correct answers already.
If you'd like to avoid using cast and convert, define your variables as real, for example. Of if you want to stick to explicit usage of numbers, simply put it this way:
select 19.00/12.00
Use CAST
.
SELECT CAST((CAST(19 AS DECIMAL(9, 2)) / CAST(12 AS DECIMAL(9,2))) AS DECIMAL(9, 2))
This will convert the result each input number to a DECIMAL
and also output the result as a DECIMAL
.
The output of the above SQL is:
1.58
Or, simply do as @gbn (the short-method) has put:
SELECT CAST(1.00 * 19 / 12 AS DECIMAL(9, 2))
actually he only need to cast the numeratior:
select cast(cast(19 as numeric(5,2))/12 as numeric(5,2))