Tuesday, October 22, 2013

Get data inside text file (.txt) that stored in Image Column

Create a new text file named Text File.txt located at D:\MSSQL.

Enable Ad hoc query for SQL Server
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO

EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO

Create a table with image data type column
CREATE TABLE TextFile(TextFileData image)

Insert the text file as a whole into SQL Server
INSERT INTO TextFile(TextFileData)
SELECT * FROM
OPENROWSET(BULK N'D:\MSSQL\Text File.txt', SINGLE_BLOB) AS Document

Check the data are inserted correctly

Now, convert the data into readable sentence.
DECLARE @Output VARCHAR(MAX) = (SELECT CAST(CAST(TextFileData AS VARBINARY(MAX)) AS VARCHAR(MAX)) FROM TextFile)


PRINT @Output

Desired Output

No comments:

Post a Comment