June 02, 2009

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


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

 
  Feedback:

comments

2 Responses to "Generate a HASH value using SQL Server 2005/2008"
  1. jbtatro said...
    November 9, 2011 at 7:46 AM

    the function only uses the first 30 characters of your string

  2. jbtatro said...
    November 9, 2011 at 2:38 PM

    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

 

Copyright © 2009-2014 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions