February 7, 2018 at 3:39 pm
Hello,
I would like to be able to update a varbinary field (named Varbinary_Data of varbinary(max) type) with a value copied from another varbinary field. I have to do it with a script, and I cannot use a subquery to get the new data in the update statement.
The data I want to insert in the field looks like 0x504B030400...D04 and it has 43,679 digits (an odd number).
I am using this update statement:
update ImageStore
set Varbinary_Data = CONVERT(varbinary(max), 0x504B030400...D04, 1)
where Id = 1001
The result is that in the updated field the data has an extra leading zero after the leading 0x, like: 0x0504B030400...D04, which makes the image unusable.
Is there a way to do the update avoiding that extra zero to be added? Thanks, Paul
February 7, 2018 at 4:02 pm
pavel_hant - Wednesday, February 7, 2018 3:39 PMHello,I would like to be able to update a varbinary field (named Varbinary_Data of varbinary(max) type) with a value copied from another varbinary field. I have to do it with a script, and I cannot use a subquery to get the new data in the update statement.
The data I want to insert in the field looks like 0x504B030400...D04 and it has 43,679 digits (an odd number).
I am using this update statement:
update ImageStore
set Varbinary_Data = CONVERT(varbinary(max), 0x504B030400...D04, 1)
where Id = 1001The result is that in the updated field the data has an extra leading zero after the leading 0x, like: 0x0504B030400...D04, which makes the image unusable.
Is there a way to do the update avoiding that extra zero to be added? Thanks, Paul
Why can't you use a subquery?
February 7, 2018 at 4:41 pm
I cannot use a subquery because I have to send the update script to multiple users to run it against their databases.
February 7, 2018 at 6:29 pm
pavel_hant - Wednesday, February 7, 2018 3:39 PMHello,I would like to be able to update a varbinary field (named Varbinary_Data of varbinary(max) type) with a value copied from another varbinary field. I have to do it with a script, and I cannot use a subquery to get the new data in the update statement.
The data I want to insert in the field looks like 0x504B030400...D04 and it has 43,679 digits (an odd number).
I am using this update statement:
update ImageStore
set Varbinary_Data = CONVERT(varbinary(max), 0x504B030400...D04, 1)
where Id = 1001The result is that in the updated field the data has an extra leading zero after the leading 0x, like: 0x0504B030400...D04, which makes the image unusable.
Is there a way to do the update avoiding that extra zero to be added? Thanks, Paul
You can't save an odd number of digits because VARBINARY only saves whole bytes, which are represented by two nibbles with each nibble being represented by a hex character. There's no way around it.
--Jeff Moden
Change is inevitable... Change for the better is not.
February 9, 2018 at 2:05 pm
Heh, likely you're getting incorrect data to begin with.
43,679 is the max number of characters you can copy/paste from a string in an SSMS grid, a fun old connect item that MS never got around to fixing.
I highly doubt that very specific number is a coincidence, so probably the hex value is just already wrong.
You can work around this by casting the string as XML and then copying it out.
Cheers!
August 20, 2019 at 7:27 pm
Hi!
I know it's been a long time but i had the same problem.
I was able to copy the hole content by using an SELECT statement.
Something like this:
UPDATE
TABLE1
SET
IMAGE = (SELECT IMAGE FROM TABLE2 WHERE ID = 9999)
WHERE
ID = 9999
In my case i was able to make an SELECT from a linked server.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply