Convert image to nvarchar(max)

Viewed 1831

I have a field that is defined as longblob in an MySQL database in which I upload pdf/word files/ etc. However, I access this database from my SQL Server database and the data is accessed from a linked server and views.

Problem The problem is, the data needs to be imported by another application, but the data type of the document file is coming across as image and that is causing a problem.

What have I tried

I have first tried to see if I change longblob to varbinary to get a varbinary(max) data type in SQL Server. That did not work, I still got image.

Then I tried to make a view of the view to convert the data. I tried CAST(fileContent as varchar(max)) and got the error:

Explicit conversion from data type image to nvarchar(max) is not allowed.

I then tried cast(cast(fileContent as varbinary(max)) as varchar(max)) as fileContent as I see suggested but I am getting back a ton of gibberish.

%PDF-1.4 %âãÏÓ 3 0 obj <</Type /Page /Parent 1 0 R /MediaBox [0 0 595.280 841.890] /TrimBox [0.000 0.000 595.280 841.890] /Resources 2 0 R /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Contents 4 0 R>> endobj 4 0 obj <</Filter /FlateDecode /Length 521>> stream xœµRËnÛ0Ü3¿b-ÐÐ|ŠRn ) 4¥==¨¶âºu’F1

instead of something like 0x255044462D312E,

Appreciate if I could get some assistance in converting binary data (which claims it's an image to nvarchar(max).

1 Answers
declare @t table(theimage image)
insert into @t(theimage) 
values(0x3a26);


select convert(varchar(max), cast(theimage as varbinary(max)), 1) as theimagebinaryaschar
from @t;

declare @theimageaschar varchar(max) = '0x3a26';
select convert(varbinary(max), @theimageaschar, 1) as theimageasbinary;
Related