Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Searching on Binary data type columns


Searching on Binary data type columns

Author
Message
asm1212
asm1212
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 432
Hey guys,

We have this system that is not built for reporting but we are having to write reports for it anyways...

Problem:
All of the following tables are related but the id's are stored in 4 different ways

Table1 - Stored as a varchar...Example: D19377322B06194DB6A9B34667D506A6
Table2 - Stored as a binary...Example: 0xD19377322B06194DB6A9B34667D506A6
Table2 - Stored as nvarchar...Example: {E867D837-EB0C-4EDB-B56E-D7083C181E52}
Table3 - Strred as a uniqueidentifier...Example: E867D837-EB0C-4EDB-B56E-D7083C181E52

I have to get to Table4 to retrieve a column starting with Table1...I can do this manually and here is how:

1.) Get ID from Table1 -D19377322B06194DB6A9B34667D506A6
2.) Manually add 0x to that ID - 0xD19377322B06194DB6A9B34667D506A6
3.) Using that new ID and hard coding it into the WHERE clause, I select a separate id from that table : {E867D837-EB0C-4EDB-B56E-D7083C181E52}
4.) Take the brackets off of that ID - E867D837-EB0C-4EDB-B56E-D7083C181E52
5.) Use that new ID to retrieve the column I need

So doing this manually, no problem!

Problem is there are thousands of these and I cant do it manually one by one! I get stuck on step 3, in the WHERE clause: when specifying a binary, you dont use quotes, but I cant figure out how to get around that? I have tried setting the value using a variable and in the WHERE clause, I just say where ID = @num, but that doesnt work!

So my question is: How can I take a varchar value and make it a binary so I can search on it!

Any confusion as to what I am asking, let me know!

Thanks
Sean Lange
Sean Lange
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: 16542 Visits: 16997
In order to help we will need a few things:

1. Sample DDL in the form of CREATE TABLE statements
2. Sample data in the form of INSERT INTO statements
3. Expected results based on the sample data

Please take a few minutes and read the first article in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
The Dixie Flatline
The Dixie Flatline
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3398 Visits: 6899
You can do the conversions on the fly in a single query by using expressions in your joins and where clauses. It may be slower than joins using the same datatypes, but definitely faster than doing it by hand.


declare @table1 table ( value1 varchar(50) )
insert into @table1 select 'D19377322B06194DB6A9B34667D506A6'

declare @table2 table ( value2 binary(50), value3 nvarchar(50))
insert into @table2 select convert(binary,'0xD19377322B06194DB6A9B34667D506A6'), '{E867D837-EB0C-4EDB-B56E-D7083C181E52}'

declare @table3 table ( value4 uniqueIdentifier)
insert into @table3 select 'E867D837-EB0C-4EDB-B56E-D7083C181E52'

select value1,value2,value3,value4
from @table1 t1
join @table2 t2 on t2.value2 = convert(binary,'0x'+t1.value1)
join @table3 t3 on t3.value4 = substring(t2.value3,2, len(t2.value3)-2)



__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
HowardW
HowardW
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1211 Visits: 9892
The Dixie Flatline (6/18/2013)
You can do the conversions on the fly in a single query by using expressions in your joins and where clauses. It may be slower than joins using the same datatypes, but definitely faster than doing it by hand.


declare @table1 table ( value1 varchar(50) )
insert into @table1 select 'D19377322B06194DB6A9B34667D506A6'

declare @table2 table ( value2 binary(50), value3 nvarchar(50))
insert into @table2 select convert(binary,'0xD19377322B06194DB6A9B34667D506A6'), '{E867D837-EB0C-4EDB-B56E-D7083C181E52}'

declare @table3 table ( value4 uniqueIdentifier)
insert into @table3 select 'E867D837-EB0C-4EDB-B56E-D7083C181E52'

select value1,value2,value3,value4
from @table1 t1
join @table2 t2 on t2.value2 = convert(binary,'0x'+t1.value1)
join @table3 t3 on t3.value4 = substring(t2.value3,2, len(t2.value3)-2)



Need to be careful doing conversions of Varchars into Binary.

With the default style, you'll end up with a binary representation of the varchar, not the conversion of the Hex to Binary, which is what you actually want.

Note the differences here:


DECLARE @HexVarchar VARCHAR(32)='D19377322B06194DB6A9B34667D506A6'
SELECT convert(VARBINARY,@HexVarchar)
--0x443139333737333232423036313934444236413942333436363744353036
SELECT convert(VARBINARY,@HexVarchar,2)
--0xD19377322B06194DB6A9B34667D506A6


geoff5
geoff5
SSC-Enthusiastic
SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)

Group: General Forum Members
Points: 180 Visits: 543
Here is one option. I used a special qualifier of the CONVERT() function to get the correct binary value out of the VARCHAR value in the first table.

with
Table1 as
(select ID_VARCHAR = 'D19377322B06194DB6A9B34667D506A6'),
Table2 as
(select ID_BINARY = cast(0xD19377322B06194DB6A9B34667D506A6 as binary(16)),
ID2_NVARCHAR = cast('{E867D837-EB0C-4EDB-B56E-D7083C181E52}' as nvarchar(40))),
Table3 as
(select ID2_GUID = cast('E867D837-EB0C-4EDB-B56E-D7083C181E52' as uniqueidentifier))

select
*
from
Table1 inner join
Table2 on convert(binary(16), Table1.ID_VARCHAR, 2) = Table2.ID_BINARY inner join
Table3 on convert(uniqueidentifier, Table2.ID2_NVARCHAR) = Table3.ID2_GUID


The Dixie Flatline
The Dixie Flatline
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3398 Visits: 6899
I stand corrected. Thanks, guys.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
asm1212
asm1212
SSC-Enthusiastic
SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)SSC-Enthusiastic (194 reputation)

Group: General Forum Members
Points: 194 Visits: 432
HowardW (6/18/2013)
The Dixie Flatline (6/18/2013)
You can do the conversions on the fly in a single query by using expressions in your joins and where clauses. It may be slower than joins using the same datatypes, but definitely faster than doing it by hand.


declare @table1 table ( value1 varchar(50) )
insert into @table1 select 'D19377322B06194DB6A9B34667D506A6'

declare @table2 table ( value2 binary(50), value3 nvarchar(50))
insert into @table2 select convert(binary,'0xD19377322B06194DB6A9B34667D506A6'), '{E867D837-EB0C-4EDB-B56E-D7083C181E52}'

declare @table3 table ( value4 uniqueIdentifier)
insert into @table3 select 'E867D837-EB0C-4EDB-B56E-D7083C181E52'

select value1,value2,value3,value4
from @table1 t1
join @table2 t2 on t2.value2 = convert(binary,'0x'+t1.value1)
join @table3 t3 on t3.value4 = substring(t2.value3,2, len(t2.value3)-2)



Need to be careful doing conversions of Varchars into Binary.

With the default style, you'll end up with a binary representation of the varchar, not the conversion of the Hex to Binary, which is what you actually want.

Note the differences here:


DECLARE @HexVarchar VARCHAR(32)='D19377322B06194DB6A9B34667D506A6'
SELECT convert(VARBINARY,@HexVarchar)
--0x443139333737333232423036313934444236413942333436363744353036
SELECT convert(VARBINARY,@HexVarchar,2)
--0xD19377322B06194DB6A9B34667D506A6




I used this and I had tried the CONVERT function before but I was getting that 1st result with the CONVERT...Didnt know or think about putting a style in there! But once I did that, worked to perfection, thanks guys!
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