Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why Image inserts different value in bytes

I have a table with a column of type Image and I’m trying to insert the value in bytes using a SQL query like this:

INSERT Into DocumentImage (FileName, DocImage, DateTimeAdded)
VALUES ('TestTemp.xlsx', 
        '0x504B030414000600080000002100ECC0AA13A901000035070000130008025B436F6E74656E745F54797065735D2E786D6C20A2040228A0000200000000', 
        GETDATE())

But after inserting the Image value changes to some other bytes like this

'0x30783530344230333034313430303036303'

Any idea why this is happening or how can I store the expected value? Would appreciate any suggestion. Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your column is of type image (a poor-mans varbinary(max)) and you’re trying to insert a string literal into it. You need to remove the single quotes around your string, because right now it’s implicitly converting the string literal ‘0x504…’ to varbinary, which isn’t what you want.

You can see the effects of different types of inserts into an image column:

drop table if exists #vb
create table #vb
(
    id int identity(1,1),
    vb image
)

insert into #vb (vb) select '0x504'
insert into #vb (vb) select 0x504
insert into #vb (vb) select 'ThisWillGetConvertedToBinary'

Note, if your column were actually varbinary (which I know, you said you can’t change the schema) it wouldn’t allow this oversight to take place, and instead would throw an exception:

7, Level 16, State 3, Line 9
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading