Link to home
Start Free TrialLog in
Avatar of John Saraka
John SarakaFlag for United States of America

asked on

calculate MD5 or SHA checksum of each row from table fields

How to calculate MD5 or SHA checksum of each row from table fields in SSIS

Avatar of John Saraka
John Saraka
Flag of United States of America image

ASKER

I am trying to write C# script to calculate MD5 hash for each row for certain fields in the table.
Avatar of Pavel Celba
You may use HASHBYTES() T-SQL function and pass the concatenated columns as a parameter to it. You may use CONCAT() function to concatenate selected columns.
pcelba provides the solution, but expand for people looking into this and to give caution/context, you can use CHECKSUM or BINARY_CHECKSUM to generate a checksum for a row which I have seen in some databases.  While easier to work with, this is not the correct approach and your approach with generating a hash is the correct way (I would go at least SHA1 to avoid collisions).  

Using CHECKSUM or BINARY CHECKSUM you will have collisions, lots of them.  For example:
SELECT CHECKSUM(1.23),CHECKSUM(-1.23)

Open in new window

both have a result of 2001159322.  So don't use these for any data version type requirement.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.