Solved

SQL Server 2000 Truncate Decimal Places

Posted on 2008-06-23
6
5,142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 13

Assisted Solution

by:rickchild
rickchild earned 100 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 69

Accepted Solution

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

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

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

627 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