Find out which value in table is in error

  • I know which column is erroring out. It is varchar and I am trying to convert it to decimal. But do we know how to find out which value is

    erroing out. It is kind of big table.

    Error converting data type varchar to numeric.

  • SELECT * FROM tbl WHERE ltrim(rtrim(col)) NOT LIKE '%[^0-9]%'

    Although this will also spit out negative numbers.

    On SQL 2012 this is easier where you can use

    SELECT * FROM tbl WHERE try_convert(int, col) IS NULL

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • You could try this:

    select * from YourTable where isnumeric(YourColumn) = 0

    this will at least show rows that are not in proper numeric format. ISNUMERIC can sometimes be fooled but its worth a try.

    The probability of survival is inversely proportional to the angle of arrival.

  • you also have to check for empty strings when converting to decimal as well

    SELECT * FROM tbl WHERE ltrim(rtrim(col)) NOT LIKE '%[^0-9]%' OR ltrim(rtrim(col))=''

    empty string converts to zero for integer, but errors for decimals...

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 4 posts - 1 through 3 (of 3 total)

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