Link to home
Start Free TrialLog in
Avatar of alexisbr
alexisbr

asked on

SQL Server 2000 Truncate Decimal Places

Hi!  I created the following code in a stored procedure in SQL Server 2000.  It works okay if the values I'm converting don't go above the precision and scale for the field. If there are more decimal places than indicated in the scale, I get the error:
"Server: Msg 8115, Level 16, State 6, Procedure spMoveData, Line 14
Arithmetic overflow error converting float to data type numeric.
The statement has been terminated."

Here's the insert statement which starts on line 14:
Insert DataTable(SID,SDate, STime, A,CC, TimeInHours, V, L, Tester)
Select SID, SDate, STime,
CAST(cast(Value1 as float) AS DECIMAL(9, 5)),
CAST(cast(Value2 as float) AS DECIMAL(10, 6)),
CAST(cast(Value3 as float) AS DECIMAL(10, 5)),
CAST(cast(Value4 as float) AS DECIMAL(7, 3)),
CAST(cast(Value5 as float) AS DECIMAL(10,5)),
Tester
from DataTemp

Is there way to just truncate the decimal places if there are more decimal places in the number being loaded than the field is set up?  I don't need any more dec places to the right than what is indicated in the cast statement.  For example, if value4, which can be (7,3) has 4 decimal places, I just want to truncate the 4th decimal place to make it 3 decimal places.

Thanks for your help.
Alexis
SOLUTION
Avatar of rickchild
rickchild
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If that doesn't work, you may find you don't need to even use a CAST() or a CONVERT(), as Float to decimal is an Implicit conversion.
Avatar of alexisbr
alexisbr

ASKER

Thanks.  I forgot to note that value1, value2, etc are all varchar fields before I convert the  values.  For example, one of the value5 fields is 1.000E-008, which does not fit in decimal (10,5).

I have to sign off now and won't be able to get back on this system until Wed am.  I just wanted to tell you so you don't wonder why I don't answer right away.  

Thanks for your help.
Alexis
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help.  I am still working on applying your suggestions but I discovered the major problem was a value that shouldn't be used in our testing so I will be filtering that range of numbers so they do not get included in the conversion.  I will keep your info handy though as it still may be needed.

Regards,
Alexis