Decimal Fields

  • I am new to sql server/Tsql etc so my apologies if this seems a laughably silly question.

    I have two fields of type smallint and a third field of type decimal(length 9 precision 18). I won't need the smallint fields if I could figure a way of dividing field one by field two and putting the result in the decimal field.

    I have written a procedure that does this but it will not put in the decimal places. when I view the table it appears that the figures have been round down.

    How can I add decimal figures to the decimal field without the number being round down?

    Thanks in advance

  • You need to use CONVERT to convert one of the smallints to decimal:

    select convert(decimal(18,8),var1)/var2

    Jeremy

  • SQL Server is rounding because you are performing the maths on 2 smallint fields.

    If you convert your smallint fields to decimal before doing the maths, it will not round. e.g.

    declare @a smallint

    declare @b-2 smallint

    declare @c decimal(18,2)

    declare @a1 decimal(18,2)

    declare @b1 decimal (18,2)

    set @a=100

    set @b-2=3

    set @c=@a/@b

    print @c --this will be rounded down to 33.00

    set @a1=@a

    set @b1=@b

    set @c=@a1/@b1

    print @c --this should give the answer unrounded

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply