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


Match Varbinary(max) with another Varbinary(max)


Match Varbinary(max) with another Varbinary(max)

Author
Message
Chirag#
Chirag#
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 59
i have a table with one binary column. that contains 0x00010100000101010101......00 column datalength is 35040, i am prepare this data like

@Jobbyte = COALESCE(@Jobbyte , 0x) + Cast ((Case When Sum(A.bit) >= 1 then 1 else 0 end)as binary(1))

i have to compare this binary data with another binary data and get the matching binary count. both the binary data has equal length of data

Please look below image, their is 2 binary data Binary1 and Binary2 i want to compare both binary data and get Sum of matching binary data. only one condition is when binary2's single bit is 0x01



Last Row is indicate 0 = No Match, 1 = Match and last column is Sum of Last row (4) Please suggest me how can i compare , it's better if you can post a query
Erland Sommarskog
Erland Sommarskog
SSCarpal Tunnel
SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)SSCarpal Tunnel (5K reputation)

Group: General Forum Members
Points: 4960 Visits: 875
My head spins...

If I understand it correctly, you have a table with bit values and you want to make some sort of comparison, and you think that a varbinary(MAX) is not what you need. But I'd say that most likely it isn't.

Why don't you present your original problem instead, and might be able to find a solution. A tip: you help yourself enormously if you post:

1) CREATE TABLE statements for your table(s).
2) INSERT statements with sample data.
3) The desired result given the sample.
4) A short description of the business rules.

This way can copy the script you post to develop a tested solution.

Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)SSC Guru (205K reputation)

Group: General Forum Members
Points: 205303 Visits: 41952
chirag ~ (9/2/2013)
i have a table with one binary column. that contains 0x00010100000101010101......00 column datalength is 35040, i am prepare this data like

@Jobbyte = COALESCE(@Jobbyte , 0x) + Cast ((Case When Sum(A.bit) >= 1 then 1 else 0 end)as binary(1))

i have to compare this binary data with another binary data and get the matching binary count. both the binary data has equal length of data

Please look below image, their is 2 binary data Binary1 and Binary2 i want to compare both binary data and get Sum of matching binary data. only one condition is when binary2's single bit is 0x01



Last Row is indicate 0 = No Match, 1 = Match and last column is Sum of Last row (4) Please suggest me how can i compare , it's better if you can post a query


Please see the first "Helpful Link" in my signature line below. You don't have to post a 35KByte binary but a couple of dozen bytes would be helpful.

--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
mickyT
mickyT
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2732 Visits: 3318
Hi

To me it looks like you want to do a & (bitwise AND) on the binaries. Unfortunately for the length of the binaries that you are indicating this will not work in SQL Server. Splitting into chunks of 8 appears to work OK, but this would need to be thoroughly tested.
Also this looks really ugly and there is bound to be a better way.

with sampleBins as (
select
bin1 = cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1))
,
bin2 = cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1)) + cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1))
+ cast(0 as binary(1)) + cast(1 as binary(1)) + cast(1 as binary(1)) + cast(0 as binary(1))
)
select
SUM(LEN(REPLACE(REPLACE(
CONVERT(varchar(MAX),
CAST(
CAST(SUBSTRING(b.bin1,((N - 1)*8)+1,8) as bigint)
&
CAST(SUBSTRING(b.bin2,((N - 1)*8)+1,8) as bigint)
as varbinary(MAX))
,1)
,'0',''),'x','')))
from sampleBins b
cross apply (select top (((datalength(b.bin1) - 1) / 8) + 1) N From Tally) t


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