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


Is it reliable to compare HASHBYTES for equality when CAST as VARCHAR?


Is it reliable to compare HASHBYTES for equality when CAST as VARCHAR?

Author
Message
Bruce Hendry
Bruce Hendry
SSC Journeyman
SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)SSC Journeyman (92 reputation)

Group: General Forum Members
Points: 92 Visits: 259
SSIS does not allow comparison of the varbinary data type; the output of HASHBYTES. So if I want to compare source and destination HASHBYTES within the Conditional Split component I must convert to string, e.g. (DT_STR,8000,1252)SourceHashbytes != (DT_STR,8000,1252)DestinationHashbytes. In this case, I'm using SHA1 so SourceHashbytes and DestinationHashbytes are varbinary(20).

I'm not sure about a few things:
- What's the max varchar (DT_STR) length I need to use in order to compare varbinary(20) when it is CAST as a string, i.e. what does varbinary(20) translate to as a string? I've looked at the string output and I'm definitely not in danger of exceeding 8000 characters/appx bytes. The lengths do vary, however, and I'd like to understand how that works.
- Is this a reliable comparison, i.e. will the varchar conversion output always contain reproducible, unique values for the varbinary input?
- Am I taking it on the performance nose forcing the compare in SSIS using strings; would it be better to do it in t-sql (where I would now have to deal with crossing source/destination boundaries in the same stored proc)?

Any insight appreciated, thanks.
robert.gerald.taylor
robert.gerald.taylor
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1802 Visits: 1491
Bruce Hendry (4/16/2013)
SSIS does not allow comparison of the varbinary data type; the output of HASHBYTES. So if I want to compare source and destination HASHBYTES within the Conditional Split component I must convert to string, e.g. (DT_STR,8000,1252)SourceHashbytes != (DT_STR,8000,1252)DestinationHashbytes. In this case, I'm using SHA1 so SourceHashbytes and DestinationHashbytes are varbinary(20).

I'm not sure about a few things:
- What's the max varchar (DT_STR) length I need to use in order to compare varbinary(20) when it is CAST as a string, i.e. what does varbinary(20) translate to as a string? I've looked at the string output and I'm definitely not in danger of exceeding 8000 characters/appx bytes. The lengths do vary, however, and I'd like to understand how that works.
- Is this a reliable comparison, i.e. will the varchar conversion output always contain reproducible, unique values for the varbinary input?
- Am I taking it on the performance nose forcing the compare in SSIS using strings; would it be better to do it in t-sql (where I would now have to deal with crossing source/destination boundaries in the same stored proc)?

I do the same thing in my SSIS packages; SHA1 produces varbinary(20) which would convert to a string that's 40 characters long (two characters to represent one byte (00 to FF).

From a conditional split:
Saved_Type1Hash != (DT_WSTR,40)Calc_Type1Hash

HTH,
Rob
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