Extract pdf file from a sql db

  • I have pdf files stored in my sql db and would like to extract them to a folder. I have a script that will list them all but don't know how to actually write them to the folder. Can someone help me finish this up?

    declare @path varchar(100)= 'c:\images\'

    Select [CreatedDate], subject, @path+ltrim(subject)+'_'+filename as path

    from pdftable where subcategory='Finished Goods Certification' and (CreatedDate >= CONVERT(DATETIME, '2014-08-01 00:00:00', 102))

    order by CreatedDate


  • certainly possible, but you probably need a bit more information.

    this example here, you could adapt, and is assuming there is an identity/primary key to the table.

    If you can install CLR on a database, i personally use a version of Elliot Witlows Common Language Runtime example he published here:



    -- Parameters: @FilePath,@FileName,@FileBytes

    -- purpose: given an varbinary image column in a table, write that image to disk

    -- usage:

    --assumes table and the file from the example above for dbo.MFGetFileImage exists already.

    declare @myfile varbinary(max)

    SELECT @myfile = rawimage FROM myImages WHERE id = 1

    EXEC dbo.MSPSaveFileImage 'C:\Data','spinning.gif',@myfile


    --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!

  • Hi, Mark.

    Thanks for sharing your problem. When it comes to pdf processing, I have another question to ask you. I wonder whether text extraction from pdf files[/url] is much simpler than pdf to text conversion[/url] process. Thre's something wrong with my pdf viewer. I want to look for a method to help with the relevant process. Any suggestion will be appreciated. Thanks in advance.

    Best regards,


  • So I am new to SQL forums, so please bear with me. I have a similar issue. I have PDF's and JPG's stored within a table. Each record is associated with a session number as the number of PDF or JPG files per "session" can vary. I would like to export the files using the filename within the record for each file in a directory named using the session number. Any ideas/concerns?

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

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