2

I have a SQL table in which some columns, when viewed in SQL Server Manager, contain <Unable to read data>. Does anyone know how to query for <Unable to read data>? I can individually modify the data in this column with update table set column = NULL where key = 'value', but how can I find whether additional rows exist with this bad data?

16
  • Which data cannot be read? Is it the primary key? Have you tried delete from myTable where KeyField='KeyValue'? What was the response?
    – jerry
    May 23, 2012 at 17:50
  • There are 280 fields in the table and total 9 rows of data present. In the 9th row half fields are filled properly but in all of the remaining fields it is written <Unable to read data>. There are strings,integers and image fields present in the table. I tried to write NULL in the field having <Unable to read data>, but it is disabled (letters are dim) and it is not allowing to to this and the row is not even getting deleted.
    – Dev
    May 24, 2012 at 2:19
  • I ran into a row like this, myself. In SQL Server Management Studio, viewing the data for the row, a column shows <Unable to read data>. How can I query for these rows? Jun 6, 2012 at 20:33
  • 2
    According to MSDN: Precision and scale can not always be preserved. For example, the Results pane supports a precision of 27. If data is of a data type with a greater precision, the data may be truncated or may be represented by <Unable to read data>.
    – Bridge
    Jun 6, 2012 at 21:08
  • 1
    @Jonathon : There is an inbuilt function called ISDATE which determines whether an input expression is a valid date or not .Select * from YourTable where ISDATE(Col)!=1 .This will result in all the rows which does not have date in correct format .
    – praveen
    Jun 7, 2012 at 1:33

4 Answers 4

9
+250

I would recommend against replacing the data. There is nothing wrong with it, is just that SSMs cannot display it properly in the Edit panel. The data in the database itself is perfectly fine, from your description.

This script shows the problem:

create table test (id int not null identity(1,1) primary key, 
    large_value numeric(38,0));
go

insert into test (large_value) values (1);
insert into test (large_value) values (12345678901234567890123456789012345678);
insert into test (large_value) values (1234567890123456789012345678901234567);
insert into test (large_value) values (123456789012345678901234567890123456);
insert into test (large_value) values (12345678901234567890123456789012345);
insert into test (large_value) values (1234567890123456789012345678901234);
insert into test (large_value) values (123456789012345678901234567890123);
insert into test (large_value) values (12345678901234567890123456789012);
insert into test (large_value) values (1234567890123456789012345678901);
insert into test (large_value) values (123456789012345678901234567890);
insert into test (large_value) values (12345678901234567890123456789);
insert into test (large_value) values (NULL);
go

select * from test;
go

The SELECT will work fine, but showing the Edit Top 200 Rows in object explorer will not:

UnableToEditData

There is a Connect Item for this issue. SSMS 2012 still exhibits the same problem.

If we look at the Numeric and Decimal details we'll see that the problem occurs at a weird boundary, at precision 29 which is actually not a SQL Server boundary (precision 28 is):

Precision   Storage bytes
1 - 9   5
10-19   9
20-28   13
29-38   17

If we check the .Net (SSMS is a managed application) decimal precision table we can see quickly where the crux of the issue is: Precision is 28-29 significant digits. So the .Net decimal type cannot map high precision (>29) SQL Server numeric/decimal types.

This will affect not only SSMS display, but your applications as well. Specialized applications like SSIS will use high precisions representation like DT_NUMERIC:

DT_NUMERIC An exact numeric value with a fixed precision and scale. This data type is a 16-byte unsigned integer with a separate sign, a scale of 0 - 38, and a maximum precision of 38.

Now back to your problem: you can discover invalid entries by simply looking at the value. Knowing that the C# representation range can accommodate values between approximate (-7.9 x 1028 to 7.9 x 1028) / (100 to 28)` (the range depends on the scale) you can search for values outside the range on each column (the actual values to search between will depend on the column scale). But that begs the question 'what to replace the data with?'.

I would recommend instead using dedicated tools for import export, tools that are capable of handling high precision numeric values. SSIS is the obvious candidate. But even the modest bcp.exe would also fit the bill.

BTW if your values are actually incorrect (ie. true corruption) then I would recommend running DBCC CHECKTABLE (...) WITH DATA_PURITY:

DATA_PURITY

Causes DBCC CHECKDB to check the database for column values that are not valid or out-of-range. For example, DBCC CHECKDB detects columns with date and time values that are larger than or less than the acceptable range for the datetime data type; or decimal or approximate-numeric data type columns with scale or precision values that are not valid.

For databases created in SQL Server 2005 and later, column-value integrity checks are enabled by default and do not require the DATA_PURITY option. For databases upgraded from earlier versions of SQL Server, column-value checks are not enabled by default until DBCC CHECKDB WITH DATA_PURITY has been run error free on the database. After this, DBCC CHECKDB checks column-value integrity by default.

Q: How can this issue arise for a datetime column?

use tempdb;
go

create table test(d datetime)

insert into test (d) values (getdate())

select %%physloc%%, * from test;

-- Row is on page  0x9100000001000000

dbcc traceon(3604,-1);

dbcc page(2,1,145,3);

Memory Dump @0x000000003FA1A060
0000000000000000:   10000c00 75f9ff00 6aa00000 010000             ....uùÿ.j .....
Slot 0 Column 1 Offset 0x4 Length 8 Length (physical) 8

dbcc writepage(2,1,145, 100, 8, 0xFFFFFFFFFFFFFFFF)

Edit top 200

dbcc checktable('test') with data_purity;

Msg 2570, Level 16, State 3, Line 2 Page (1:145), slot 0 in object ID 837578022, index ID 0, partition ID 2882303763115671552, alloc unit ID 2882303763120062464 (type "In-row data"). Column "d" value is out of range for data type "datetime". Update column to a legal value.

4
  • How can this issue arise for a datetime column? Jun 8, 2012 at 22:20
  • 2
    @MartinSmith see my update. Kids, don't do this at home :) Don't willingly corrupt your database using DBCC WRITEPAGE... Jun 8, 2012 at 22:46
  • Excellent answer, especially the reproduction by corrupting datetime data! Please mention that errors found with dbcc checktable can be removed by running dbcc checktable('test') repair_allow_data_loss. Jun 11, 2012 at 12:42
  • @Jonathon: I really think allow_data_loss is the last resort of the last resort... I much rather attempt first to restore data from a good backup. Jun 11, 2012 at 12:47
1

As suggested above ,these errors usually occurs when Precision and scale are not preserved .If your comfortable with SSIS then you can achieve to get those rows which are corrupt .Taking the values which Martin Smith created

CREATE TABLE T(ID int ,C DECIMAL(38,0));
INSERT INTO T VALUES(1,9999999999999999999999999999999999999) 

The above table reproduces the error . Here the first column represents the primary key . I inserted around 1000 rows out of which few were corrupted values . Below is the SSIS package design

SSIS Design

In the Data Conversion ,i took the column C which had errors and tried to cast it to Decimal(38,0) .Since a conversion or truncation error will occur ,therefore i redirected the error rows to an OLEDB command which basically updates the table and sets the column to NULL

Update T
Set C=NULL
where ID=?

The value of C and ID will be directed to oledb command .In case if there is no error then i'm just inserting into a table ( Actually no need to do this ).This will work if you have a primary key column in your table .

In case if there is any error in date time column a sql query can be written to verify the format of datetime values .Please go through the MSDN link for valid date time value

   Select * from YourTable where ISDATE(Col)!=1
4
  • To be clear there is nothing corrupt about 9999999999999999999999999999999999999 it is just too long for SSMS to display. Jun 7, 2012 at 7:31
  • I actually didnt meant as a corrupt data but its just that sql server supports a precision of 27 so inserting a value like the one above can result in truncation
    – praveen
    Jun 7, 2012 at 13:40
  • When running select * from table where ISDATE(col) != 1, I receive An error occurred while executing batch. Error message is: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. Looks like that query won't work for invalid data. Jun 7, 2012 at 18:55
  • @Jonathan:I hope this is wat you are looking for stackoverflow.com/questions/5124187/…
    – praveen
    Jun 8, 2012 at 13:31
0

I think you can fetch data with cursor. please try again with cursor query such as below query :

DECLARE VerifyCursor CURSOR FOR
SELECT *
FROM MyTable
WHILE 1=1 BEGIN
    BEGIN Try
        FETCH FIRST FROM VerifyCursor INTO @Column1, @Column2, ...

        INSERT INTO @MyTable2(Column1, Column2,...)
        VALUES (@Column1, @Column2, ...)
    END TRY
    BEGIN CATCH
    END CATCH
    IF (@@FETCH_STATUS<>0) BREAK
End
OPEN VerifyCursor
CLOSE VerifyCursor
DEALLOCATE VerifyCursor
0

Replacing the bad data is simple with an update:

UPDATE table SET column = NULL WHERE key_column = 'Some value'
1
  • I think the question was to find the bad rows in the column .What if there are several rows with bad data ..!
    – praveen
    Jun 9, 2012 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.