Solved

Arithmetic overflow error converting varchar to data type numeric.

Posted on 2008-06-16
4
3,457 Views
Last Modified: 2012-06-21
Hello,

I have a table which consist of 7 million rows & 110 columns with data type set to nvarchar (originally taken imported using SSIS + flat file) in SQL Server 2005.  My goal is to give the proper data types to each column, however I am running into problems when trying to convert/cast to numeric format.

I figured out that the fact that the data is currently in nvarchar was giving me problems, so I did a 'Create Table' (set the data types accordingly one 3x varchar & rest numeric(15, 8)), then INSERT INTO > SELECT cast( column AS numeric (15,8))...etc.

Received errors such as 'Arithmetic overflow error converting varchar to data type numeric'.

I did a search(using select) for TABS, '*', '(' or any other charchers and could not find anything in this one column which i isolated out of the 110. Also not this particular column has only whole number currently, however I would think '0' would be patted for now.

I understand that using the numeric (e.g 'numeric (15,8)' ) data type that 15 represents the total amount of numbers in my recordset and the 8 (part of the 15) would be my precision (making the largest number to be 9999999.99999999).

Questions:  What am I doing wrong?  Is there another method for casting correctly? Why does MS have to make something so easy so complicated.


0
Comment
Question by:RunnerNwizard
4 Comments
 
LVL 11

Accepted Solution

by:
CMYScott earned 250 total points
ID: 21799223
there is also CONVERT - however I don't believe it will give you any additional functionality to solve your issue

in the case where there is some value that cannot be converted to numeric, you might want to use the
ISNUMERIC function

you could either use it in your INSERT like this..

INSERT INTO ...... SELECT Cast(column as numeric(15,8)) FROM yourTable
WHERE ISNUMERIC(column) = 1

or, you might want to use it to find values in the table that cannot be converted to numeric and then fix those values..

SELECT * FROM yourTable WHERE ISNUMERIC(column) = 0

keep in mind, the ISNUMERIC only tests to see if a value can be cast to one of the numeric types, it does not test to see if the value can be cast to a specific type without causing an overflow, etc.

Good Luck
0
 
LVL 2

Assisted Solution

by:howyue
howyue earned 250 total points
ID: 21801340
from ur error msg and problems statement i would say it has a very high possibility that the root of the problem is ur raw data, which i believe u already aware... u can use CMYScott method to fix the invalid data before inserting them. btw, inserting 7 millions record using insert into is not efficient, u should try to use bulk insert.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Distinct values from two tables 14 17
t-sql month question 8 42
Complex SQL 10 32
Report Builder 9 25
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 …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now