Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server 2000 Truncate Decimal Places

Posted on 2008-06-23
6
Medium Priority
?
5,167 Views
Last Modified: 2008-06-25
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
0
Comment
Question by:alexisbr
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Assisted Solution

by:rickchild
rickchild earned 400 total points
ID: 21849963
When dealing with loss of precision or scale from Decimal to Decimal you should use Convert() instead of CAST()
0
 
LVL 13

Expert Comment

by:rickchild
ID: 21850022
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.
0
 

Author Comment

by:alexisbr
ID: 21850126
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1600 total points
ID: 21857839
You can do this:

CAST(CAST(cast(Value1 as float) AS DECIMAL(38, 34)) AS DECIMAL(9, 5)),
...
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 1600 total points
ID: 21857866
You can reduce decimal places when going from DECIMAL to DECIMAL (although not non-decimal numbers, of course, so a value of 10000.xxx would still cause an error going to DECIMAL(9,5) ).
0
 

Author Comment

by:alexisbr
ID: 21867980
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question