Home Forums SQL Server 2005 T-SQL (SS2K5) Arithmetic overflow error converting varchar to data type numeric. RE: Arithmetic overflow error converting varchar to data type numeric.

  • Yeah I'll echo what Bill said. Basically, you can have a query which runs perfectly fine some times, and other times it will return an error - *even if* the record set that you get back doesn't contain any problems.

    Example:

    You have a table with two columns, one of which is a VARCHAR data type. In the column, you have the following records

    2014-01-01 15

    2014-01-02 20

    2014-01-03 33

    2014-02-02 14

    2014-02-05 23

    2014-02-15 ABC

    2014-11-15 32

    2014-12-15 12

    Suppose you wrote the following query :

    SELECT CAST(Column AS DECIMAL(18, 2))

    FROM Table

    WHERE Date < '2014-02-01'

    Sometimes that will work, and give you back the records you want. But other times, even though the record from 2014-02-15 is not being selected, it will still try to convert it, and cause an error.

    If you really must have a VARCHAR column which can contain numbers and letters, and you need to convert it, what you should do is first select those records into a temporary table, which will ensure that there are no non-numeric characters, and then run your query on the temporary table