Home Forums Programming General Suggestion/solution for using Hashbytes across entire table RE: Suggestion/solution for using Hashbytes across entire table

  • The HASHBYTES() function doesn't support a multiple column list like CHECKSUM(), however, a while back I found a solution to this by appending multiple columns after re-casting to VarBinary datatype.

    For example:

    print hashbytes('MD5',cast('John' as varbinary(99)) + cast('Doe' as varbinary(99)));

    0x9FD9F63E0D6487537569075DA85A0C7F

    You can also add a computed column that consists of a hash of multiple columns like so:

    create table Employee

    (

    EmployeeID int not null,

    EmployeeFName varchar(30) not null,

    EmployeeLName varchar(30) not null,

    EmployeeHash as hashbytes('MD5',cast(EmployeeFName as varbinary(99)) + cast(EmployeeLName as varbinary(99))) persisted

    );

    insert into Employee

    values (1,'John','Wilkerson'), (2,'Susan','Phelps-Jamseson');

    select * from Employee;

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho