Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use HASHBYTES function in SQL Server for multiple columns

Tags:

sql-server

I have a requirement wherein I have to create hashvalue which consist of all columns of a table. With Checksum this can be done easily, but Checksum is not recommended as per Microsoft:

If at least one of the values in the expression list changes, the list checksum will probably change. However, this is not guaranteed. Therefore, to detect whether values have changed, we recommend the use of CHECKSUM only if your application can tolerate an occasional missed change. Otherwise, consider using HashBytes instead. With a specified MD5 hash algorithm, the probability that HashBytes will return the same result, for two different inputs, is much lower compared to CHECKSUM.

HASHBYTES accepts only 2 parameters (algorithm type, column)

Now the problem is even though HASHBYTES is more reliable compared to checksum but there doesn't seem to be an easy way to create it on multiple columns.

An example in the checksum,

create table dbo.chksum_demo1
(
    id int not null,
    name varchar(25),
    address varchar(250),
    HashValue as Checksum (id,name,address)
    CONSTRAINT PK_chksum_demo1 PRIMARY KEY (Id)
)

How can we do the above using Hashbytes instead of checksum?

like image 946
Vikas J Avatar asked Sep 24 '18 19:09

Vikas J


People also ask

What is the use of HashBytes in SQL Server?

SQL Server has a built-in function called HashBytes to support data hashing. A good hashing algorithm has these properties: It is especially sensitive to small changes in the input. Minor changes to the document will generate a very different hash result.

How do I find multiple columns in SQL?

In SQL, selecting any column is one of the easiest things as you have to type only the SELECT command and after that, the column name and the output will be the desired column. Syntax: SELECT (Column Name) FROM (Table Name);

How do you create a hash column in SQL?

You can use MD2, MD4, MD5, SHA, or SHA1 to create hashes of your data. These algorithms are limited up to 20 bytes only. In SQL Server 2012, we have an enhancement in this function and now it supports SHA2_256, SHA2_512 algorithms that can generate 32 and 64 bytes hash codes for the respective input.

Is HashBytes reversible?

The SQL hashbytes function can use a number of different algorithms, but none of the are reversible. They are all hashing algorithms, not encryption algorithms.


4 Answers

Copying the best suggestion from the supplied link here, and adding a where to show it can be used:

select MBT.refID,
hashbytes(
    'MD5',
    (select MBT.* from (values(null))foo(bar) for xml auto)
) as [Hash]
from MyBaseTable as MBT
where MBT.SomeFilter='X'

https://www.sqlservercentral.com/forums/topic/suggestionsolution-for-using-hashbytes-across-entire-table

like image 88
Nick.McDermaid Avatar answered Oct 20 '22 14:10

Nick.McDermaid


One method is concat() the fields together along with a delimiter. For any dates format them to strings manually to control the formatting.

 HashValue as HASHBYTES('SHA2_256', CONCAT(ID,'|',name,'|',address)) 

The delimiter is needed to handle empty fields so ID:1 Name:'' Address:'12' is different from ID:1 Name:'12' Address:''.

like image 21
Brian Avatar answered Oct 20 '22 15:10

Brian


Use this:

SELECT *,    
HASHBYTES('MD5', (SELECT ID, name, address FOR XML RAW))
FROM Table1
like image 37
Don Pedro Alvares Avatar answered Oct 20 '22 15:10

Don Pedro Alvares


SELECT HASHBYTES('<algorithm>', CONCAT_WS('|', f1, f2, f3, f4 ...))
FROM Table1

algorithm>::= MD2 | MD4 | MD5 | SHA | SHA1 | SHA2_256 | SHA2_512

like image 23
Oleg Avatar answered Oct 20 '22 16:10

Oleg