Solved

Arithmetic overflow error converting varchar to data type numeric.

Posted on 2008-06-16
4
3,467 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Backup & Restore 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.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

777 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