T-SQL: Hash Different Data Types


Just a quick note: pay attention when you hash different data types. The result is different.

DECLARE @v1 VARCHAR(128) = 'This is a String';
DECLARE @v2 NVARCHAR(128) = 'This is a String';

SELECT HASHBYTES('SHA2_256', @v1) AS v1;
SELECT HASHBYTES('SHA2_256', @v2) AS v2;

Also the lower/upper case:

DECLARE @v1 NVARCHAR(128) = 'this is a String';
DECLARE @v2 NVARCHAR(128) = 'This is a String';

SELECT HASHBYTES('SHA2_256', @v1) AS v1;
SELECT HASHBYTES('SHA2_256', @v2) AS v2;

Keep it simple :-)

Leave a comment

Your email address will not be published. Required fields are marked *