Solved

SQL Server 2000 Truncate Decimal Places

Posted on 2008-06-23
6
5,116 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

770 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