Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
PATPC999
Starting Member
8 Posts |
Posted - 2010-11-25 : 09:16:17
|
Im am trying to PRINT @AVGDECLARE @NUMBERONE AS NUMARIC DECLARE @NUMBERTWO AS NUMARIC DECLARE @AVG AS DECMILE(3,1)SET = @NUMBERONE = 4854SET = @NUMBERTWO = 60 SET = @AVG = CONVERT(DECIMAL(3,1), @NUMBERONE/@NUMBERTWO)PRINT @AVGWhen I do this I getArithmetic overflow error converting numeric to data type numeric.and it PRINTs 0.00When I PRINT @AVG I want to see 80.9Help!!!!! |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-25 : 09:34:41
|
A number of typosDon't know if that's causing the issue but when I run the same thing I get 80.9Suspect this is not really what you are running - check the values and datatypes - especially @avg.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
PATPC999
Starting Member
8 Posts |
Posted - 2010-11-25 : 09:38:13
|
Yes I see NUMARIC it should read NUMERIC It is a right in the real code. still getting error |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-25 : 09:53:13
|
I've tried it on a v2000 server and it works ok.can you post the result ofselect @NUMBERONE/@NUMBERTWOand select @AVG = 99.9select @AVG==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
PATPC999
Starting Member
8 Posts |
Posted - 2010-11-25 : 10:10:08
|
DECLARE @CREDITSUM AS INTDECLARE @TOTALGRADES AS INTDECLARE @AVG AS DECIMAL(3,1) SET @TOTALGRADES = 4854 SET @CREDITSUM = 60 SET @AVG = CONVERT(DECIMAL(3,2),@TOTALGRADES/@CREDITSUM) PRINT @AVGI am new to this as you can tell when I run this code it displays 0.0 instead of 80.9Also error messageServer: Msg 8115, Level 16, State 8, Line 101Arithmetic overflow error converting numeric to data type numeric. Not sure where I am going wrong to get the decimal point to displayif I write SET @AVG = 4854/60PRINT CAST(@AVG AS CHAR(5)) I get 80 instead of 80.9 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2010-11-25 : 10:13:51
|
You are doing integer arithmetic so the result will be truncated to an integer.trydeclare @AVG decimal(4,2)SET @AVG = CONVERT(DECIMAL(3,2),1.0*@TOTALGRADES/@CREDITSUM)decimal(3,2) is 3 digits with 2 decimal places eg 8.09 - 80.9 will not fit in it.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
PATPC999
Starting Member
8 Posts |
Posted - 2010-11-25 : 10:34:54
|
I got it to work!!!Declare all three as DECIMALandSET @AVG = 1.0*(@TOTALGRADES/CREDITSSUM)PRINT @AVGI get 80.90Thanks so much!!!!!!!!!!!!!! |
|
|
|
|
|
|
|