Showing posts with label division. Show all posts
Showing posts with label division. Show all posts

Wednesday, March 21, 2012

need decimal data after division

HI,

when i divide 2 nos i need the result in decimal. always the fractional part is truncated

how to get it.

venp--


You are suffering from integer math
there are several ways to do this, take a look at this

select 3/2 --integer math

select 3/2.0 --implicit conversion

select 3/(2 *1.0) --implicit conversion
select 3/convert(decimal(9,4),2) --explicit conversion

Denis the SQL Menace
http://sqlservercode.blogspot.com/

|||

hi,

if i say

select convert(decimal(10,2),(convert(decimal(10,2),9.0)/convert(decimal(10,2),4)))

it is displaying 2.25

but i need like the below

declare @.i decimal(10,2)

set @.i= convert(decimal(10,2),(convert(decimal(10,2),9.0)/convert(decimal(10,2),4)))

select 'i'=@.i

i need the result as i=2.25, (i.e) i need to put the value in a variable.

venp--

|||

I don't understand what you mean. Can you explain more clearly? The precision & scale of any arithmetic calculation involving decimal numbers depends on the precision & scale of the individual values. You can use CAST to get constant precision/scale but make sure you use the appropriate values. In any case, see the link below from BOL for more details:

http://msdn2.microsoft.com/en-us/library/ms190476.aspx

Also, each tool (ISQLW, SSMS or SQLCMD) handles the display of decimal values differently and there are often settings to control it. In any case, you need to explain your problem with expected results. And mention the version of SQL Server you are using.

|||

Hi,

i need to divide an amount value with month, like this 10000/9 . i need the value in decimal as it involves money.i have sql server 2005 installed.

venp

|||You should then cast the amount value to decimal with appropriate precision/scale (depends on your data). And the amount column should be decimal anyway. I am not sure why it is integer.