Hi, how can i get a accurate division in tsql - is there some way of casting?
Problem is simple, "SELECT 3 / 2" is "1" - what can i do to get 1.5 as result?
When both 3 and 2 are integers, you get a division by integers, so it's rounded to 1.
You could either cast (look up CAST in BOL) to the desired datatype explicitly, or just add a decimalpoint to one of the numbers, then it will be implicitly casted.
SELECT 3 / 2.0
-
1.500000
(1 row(s) affected)
=;o)
/Kenneth
Hi
Your problem relates SQL server implicit data conversion. As 3 and 2 are integers , your result is automatically converted to an integer. You need to explicitly tell SQL server what datatype you are interested in. For examle,
SELECT (3 * 1.0)/2 -- multiply by 1.0 , implicit convertion to numeric
SELECT CONVERT(NUMERIC,3)/2 -- explicit conversion
NB.
No comments:
Post a Comment