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 :-)


About Peter Lalovsky

I am Microsoft SQL Server certified professional, creating with T-SQL, SSRS, SSIS, ASP.NET/C#, Azure, Python, PowerShell and more on a daily basis since year 2006. In 2016 i wrote a book for beginner and intermediate T-SQL programmers which you can download here. This blog is something like my personal programming documentation. When i am not in front of a computer, i am around my paper car – Trabant 601.

Leave a comment

Your email address will not be published.