0

I have a database running on SQL Server 2012 and I need to locate the tables which have varbinary column type in them.

In fact, I need to retrieve some scanned documents which have been stored in the database. I have found one table called 'ScannedDocument' and it does have a varbinary column, called 'SCD_DOCIMAGE'. However, when I run the following:

Select * from ScannedDocument

, I don't get any results in the output window in SSMS.

My first guess is that the scanned documents I am looking for are not stored in this table. What would be the T-SQL syntax to locate all tables with varbinary columns?

Also, what would be the T-SQL syntax to retrieve the documents from that column once the correct table has been identified?

John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
user3115933
  • 4,303
  • 15
  • 54
  • 94
  • Now that John has shown you how to find the column you want to extract the document found there. This is no simple feat because varbinary is just binary data. You need some details in order to have a chance. You need to what the data there represents. Is it a pdf? jpg? Maybe a word document. – Sean Lange Apr 11 '17 at 13:27

3 Answers3

4

Take a peek at INFORMATION_SCHEMA.COLUMNS

Select * From INFORMATION_SCHEMA.COLUMNS where data_type='varbinary'

To Download or Save to Disk, this may help Script to save varbinary data to disk

Community
  • 1
  • 1
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
1

SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE data_type='varbinary'

Ram Grandhi
  • 397
  • 10
  • 27
0

Aaron Bertrand has blogged about some shortcomings in the INFORMATION_SCHEMA objects.

Here's a query to identify varbinary fields using the SQL Server system objects:

SELECT OBJECT_SCHEMA_NAME(c.object_id) + '.' + OBJECT_NAME(c.object_id) AS tablename,
    c.name AS columnname,
    t.name + '(' + CASE
                       WHEN c.max_length = -1 THEN
                           'MAX'
                       ELSE
                           CONVERT(VARCHAR(10), c.max_length)
                   END + ')' AS columntype
FROM sys.columns c
    INNER JOIN sys.types t
        ON t.system_type_id = c.system_type_id
WHERE t.name = 'varbinary';
Community
  • 1
  • 1
Thomas Rushton
  • 5,746
  • 2
  • 25
  • 16