How to get a row number which is failing during conversion

  • I have about 20000 records in the SELECT clause and one of them is failing during conversion spitting out the following error message. "Arithmetic overflow error converting varchar to data type numeric.

    " Is there a way to find out which row is failing?

  • There will probably be a row that has a character that cannot be converted to integer.

    try using this code to find the offending row(s)

    SELECT * FROM YourTable

    WHERE ISNUMERIC(YourColumn)=0

  • Thanks for the reponse.

    But that is not going to work because I am only selecting the records where ISNUMERIC(mycloumn) = 1.

  • Steve, cause it's an overflow I recon it's not that there is char

    Also be careful because

    This will return a 1 for ISNUMERIC but it's not convertible: '10,0'.

    What I normally do is run a top until I find a subset of data that contains my bad value and then I go from there.

    What data type are you converting to?

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Check out the BOL entry for IsNumeric. "+", "-", ".", and currency symbols are all considered Numeric. Change your where clause to:

    Where

    Column NOT LIKE '%[^0-9]%'

    This says only allow numbers ignoring all other characters. If you want to allow decimals and negatives you can add the necessary characters to the mask.

  • Converting to deciman(12,7)

  • Okay sorry i mis-read the question..

    It seems like the value is too big to fit into a numeric column

  • Also your select statement will normally return a number of rows before it actually falls over so you could use that as a point in the DB to start looking for bad data

    e.g

    DECLARE @Tmp TABLE

    (COl1 VARCHAR(100))

    INSERT INTO @Tmp

    SELECT '1' UNION ALL

    SELECT '1111111111111111111111'

    SELECT CAST(col1 as decimal(12,7))

    FROM @Tmp

    This fails but it also return one row, so I know that it's after the first row etc...

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • The only other thing I can think of it to return all rows that have a Len(Col) greater than what your decimal can hold and look at those

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Ok assuming all your values are numeric then try this:

    in the where clause

    CHARINDEX('.',col1) > 6

    OR

    (CHARINDEX('.',col1) = 0 AND LEN(col1) > 5)

    Here is my example for returning values that would overflow.

    DECLARE @Tmp TABLE

    (COl1 VARCHAR(100))

    INSERT INTO @Tmp

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '1' UNION ALL

    SELECT '92345.1234567' UNION ALL

    SELECT '12345.12345671' UNION ALL

    SELECT '123456.1234567' UNION ALL

    SELECT '111111111111111111111111111111' UNION ALL

    SELECT '1234567890'

    SELECT

    --CAST(col1 as decimal(12,7))

    col1

    FROM @Tmp

    WHERE

    CHARINDEX('.',col1) > 6

    OR

    (CHARINDEX('.',col1) = 0 AND LEN(col1) > 5)

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Christopher,

    That hit right on the money.

    Thanks guys so much for your help.

    Appreciate it.

  • The error is because you need to specify your decimal better. When your looking at the decimal data type it is decimal(TotalPositions, RoundToThe) which is probably causing your issue. So if you had an int of 1334 and specified decimal(4, 2) well you will get an arithmetic overflow error because you forgot to add spaces for the .xx so your decimal should have been decimal(7,2). Hopefully that makes sense if you just increase your decimal corrector position you won't get that error. Good luck!

    declare @num1 int = 1334

    --select cast(@num1 as decimal(4, 2)) --This will give an error on your

    select cast(@num1 as decimal(7, 2)) -- This has correct number of positions to hold

  • Almost 3 years old thread

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply