Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Server 2000 Truncate Decimal Places

Posted on 2008-06-23
6
Medium Priority
?
5,162 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 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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

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.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

715 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