Generate a HASH value using SQL Server 2005/2008

Do you want to pass a string and generate a HASH value from it using SQL Server? Here’s a very simple way of doing so using HashBytes. Let us see this with an example:

DECLARE @SomeValue nvarchar(50);
SELECT @SomeValue = 'SQLServerCurry';
SELECT HashBytes('SHA1', @SomeValue);
GO


generates an output


0x51FD96B1BD5CE0003370551D5498BA3C0E64BE4C


Using HashBytes, you can use algorithms like MD2, MD4, MD5, SHA or SHA1. The input string can be a varchar, nvarchar or varbinary and can be upto a maximum of 8000 bytes

2 comments:

  1. the function only uses the first 30 characters of your string

    ReplyDelete
  2. sorry that was my fault, i did something like HASHBYTES('SHA1', cast( '...really large string' as nvarchar)) which when you don't specify a size it uses 30 so it was truncating its value, my bad

    ReplyDelete