SQL Server: export a binary column to a file with bcp
Extract a document stored in a binary column to a file with bcp queryout.
bcp can extract a binary column (Word document, PDF...) from a SQL Server table to a file, using queryout.
bcp "select document_binary_data from database where id = 12345" queryout "c:\filename.doc" -S server -U username -P '<PASSWORD>'
Explanation
queryoutexports the result of a query rather than a whole table.-Sis the server,-Uand-Pthe SQL login and its password (replaced here by<PASSWORD>).- The output file receives the column value; adapt its extension to the stored document type.
Notes
- Replace
database,document_binary_dataandidwith your real table and column names. - To write the binary faithfully, a format option such as
-n(native) or-fwith a format file is usually added; validate this on your version before use. - To avoid typing a password in clear text, use
-T(Windows authentication) instead of-U/-P.