Monday, 17 February 2014

The Hashbyte Function

Today I got a little problem with a developer that wants to use the Hashbyte function. After a little discussion and reading we saw that the output of the function ist varbinary so there is no way to put it into a varchar field without different results. See the Example:


So if you want to use the Hashbyte function you have to use the right data type.

Copyable Version:
DECLARE @HashThis nvarchar(40)
DECLARE @HashValue nvarchar(42)
DECLARE @HashValue2 varbinary(42)

set @HashThis = 'myteststring'
SELECT HASHBYTES('SHA1', @HashThis) as 'Auto data type'

set @HashValue=HASHBYTES('SHA1', @HashThis)
SELECT @HashValue as 'Varchar data type'

set @HashValue2=HASHBYTES('SHA1', @HashThis)
SELECT @HashValue2 as 'Varchar data type'

No comments:

Post a Comment