How can I stop this UDF truncating decimal places?

  • Dear Forum,

    The UDF I've written below does not return any decimal places. Any idea what I might be doing wrong?

    Thanks, Bill

    CREATE FUNCTION dbo.iasg (@sales_gr decimal,@sales_1 decimal,@sales_2 decimal)

    RETURNS decimal

    AS

    BEGIN

     declare @iasg decimal

    SET @iasg = 0.0

    IF (@sales_gr<-99 or @sales_1<-99 or @sales_2<-99)

     SET @iasg = -999.0

    ELSE

     SET @iasg = (@sales_gr + @sales_1 + @sales_2) / 3

    RETURN @iasg

    END 

  • Bill,

    Simple error mate.

    You need to declare scale and precision when you declare a decimal variable. Otherwise it defaults to 0.

    E.g. Declare @Variable Decimal(10[Scale - number size to right of decimal point], 2[Precision - numbers to left of decimal point])

    Have Fun.

    Best regards

    Steve

    Steve

    We need men who can dream of things that never were.

  • Thanks Steve,

    This weasel is back on track.

    Bill

  • No problems Bill.

    Only knew 'cos i'd done the same thing myself.......

    Steve

    We need men who can dream of things that never were.

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

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