title: How to use HASHBYTES function in SQL Server for multiple columns
body: <p>I have a requirement wherein I have to create hashvalue which consist of all columns of a table. With <code>Checksum</code> this can be done easily, but <code>Checksum</code> is not recommended as per Microsoft: </p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p><code>HASHBYTES</code> accepts only 2 parameters (algorithm type, column)</p>
  
  <p>Now the problem is even though <code>HASHBYTES</code> is more reliable compared to checksum but there doesn't seem to be an easy way to create it on multiple columns.</p>
  
  <p>An example in the checksum,</p>
  
  <pre><code>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)
  )
  </code></pre>
  
  <p>How can we do the above using <code>Hashbytes</code> instead of checksum? </p>