SQL Server 2005 - How do I convert image data type to character format

后端 未结 2 1425
耶瑟儿~
耶瑟儿~ 2021-01-23 13:57

Background: I am a software tester working with a test case management database that stores data using the deprecated image data type. I am relatively inexper

相关标签:
2条回答
  • 2021-01-23 14:27

    One solution (for human readability) is to pull it out in chunks that you convert from binary to character data. If every byte is valid ASCII, there shouldn't be a problem (although legacy data is often not what you expect).

    First, create a table like this:

    create table Nums(
      n int primary key
    );
    

    and insert the integers from 0 up to at least (maximum image column length in bytes)/8000. Then the following query (untested, so think it through) should get your data out in a relatively useful form. Be sure whatever client you're pulling it to won't truncate strings at smaller than 8000 bytes. (You can do smaller chunks if you want to be opening the result in Notepad or something.)

    SELECT
      yourTable.keycolumn,
      Nums.n as chunkPosition,
      CAST(SUBSTRING(imageCol,n*8000+1,8000) AS VARCHAR(8000)) as chunk
    FROM yourTable
    JOIN Nums
    ON Nums.n <= (DATALENGTH(yourTable.imageCol)-1)/8000
    ORDER BY yourTable.keycolumn, Nums.n
    
    0 讨论(0)
  • 2021-01-23 14:45

    You presumably want to convert to byte data rather than character. This post at my blog Save and Restore Files/Images to SQL Server Database might be useful. It contains code for exporting to a byte array and to a file. The entire C# project is downloadable as a zip file.

    0 讨论(0)
提交回复
热议问题