SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Updating a varbinary field - extra leading zero added


Updating a varbinary field - extra leading zero added

Author
Message
pavel_hant
pavel_hant
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 22
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)SSC Guru (241K reputation)

Group: General Forum Members
Points: 241383 Visits: 40689
pavel_hant - Wednesday, February 7, 2018 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


Why can't you use a subquery?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
pavel_hant
pavel_hant
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 22
I cannot use a subquery because I have to send the update script to multiple users to run it against their databases.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)SSC Guru (546K reputation)

Group: General Forum Members
Points: 546606 Visits: 44668
pavel_hant - Wednesday, February 7, 2018 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


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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jacob Wilkins
Jacob Wilkins
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16764 Visits: 12392
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search