1

I have a inventory column with various data and multiple rows, it's necessary replace random varbinary data in middle of column.

Example of Inventory column:

Screenshot

For example this:

0x0500420000000000005000FFFFFFFFFF56730E64FFFFFFFFFFFFFFFFFFFFFFFF0400180000000000006000FFFFFFFFFF56730E72FFFFFFFFFFFFFFFFFFFFFFFF04001E0000000000007000FFFFFFFFFF56730E5EFFFFFFFFFFFFFFFFFFFFFFFF

Need to be changed to:

0x0500420000000000005000FFFFFFFFFF56730E64FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF04001E0000000000007000FFFFFFFFFF56730E5EFFFFFFFFFFFFFFFFFFFFFFFF

I know how to change static data like here. But here is many rows and in every row data is different. It's possible maybe change data somehow by length? Start length position is 67, end 131.

4
  • When you say "random" do you really mean "variable" or do you actually want a section updated randomly? Commented Nov 5, 2021 at 14:40
  • I mean variable data in middle of column need to be changed to static data 'FF'. Commented Nov 5, 2021 at 14:49
  • 1
    And do you have a table that specifies Row with Id X should have start position 67 until end 131 masked out or where will this come from? Commented Nov 5, 2021 at 14:51
  • It's column length position, screenshot: link. Commented Nov 5, 2021 at 16:54

1 Answer 1

3

But here is many rows and in every row data is different. It's possible maybe change data somehow by length? Start length position is 67, end 131.

You can use STUFF or SUBSTRING and + to rewrite the entire blob, or you can update it in-place, see Updating Blobs, eg

drop table if exists #temp
go
create table #temp(id int, blob varbinary(max))
insert into #temp(id,blob) values (1,0x0500420000000000005000FFFFFFFFFF56730E64FFFFFFFFFFFFFFFFFFFFFFFF0400180000000000006000FFFFFFFFFF56730E72FFFFFFFFFFFFFFFFFFFFFFFF04001E0000000000007000FFFFFFFFFF56730E5EFFFFFFFFFFFFFFFFFFFFFFFF)

declare @newBytes varbinary(100) = 0xAAAAAAAAAAAA

--only for varbinary(max) but updates in-place
update #temp 
  set blob.write(@newBytes,10,datalength(@newBytes))

--for varbinary(max) or varbinary(n) replace the whole value
update #temp 
  set blob = cast(STUFF(blob,30,datalength(@newBytes),@newBytes) as varbinary(max))

select * from #temp 
Sign up to request clarification or add additional context in comments.

2 Comments

This is what can resolve issue, with blob column working fine, but with inventory column i receive error msg: "Cannot call methods on varbinary." Seems it's because i have varbinary length limit in column properties. How to solve this?
See updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.