Produces a fixed-length, hash value expression.
hashbytes(algorithm, expression[, expression...] [, using options])
Md5 (Message Digest Algorithm 5) – is the cryptographic hash algorithm with a 128 bit hash value. hashbytes('md5', expression[,...]) results in a varbinary 16-byte value.
Sha-Sha1 (Secure Hash Algorithm) – is the cryptographic hash algorithm with a 160-bit hash value. hashbytes('shal', expression[,...]) results in a varbinary 20-byte value.
Ptn – The partition hash algorithm with 32-bit hash value. The using clause is ignored for the ‘ptn’ algorithm. hashbytes('ptn', expression[,...]) results in an unsigned int 4-byte value.
lsb – all byte-order dependent data is normalized to little-endian byte-order before being hashed.
msb – all byte-order dependent data is normalized to big-endian byte-order before being hashed.
unicode – character data is normalized to unicode (UTF–16) before being hashed.
unicode_lsb – a combination of unicode and lsb.
unicode_msb – a combination of unicode and msb.
update xtable set tamper_seal=hashbytes('sha1', col1, col2, col4, @salt) -- declare @nparts unsigned int select @nparts= 5 select hashbytes('ptn', col1, col2, col3) % nparts from xtable
alter table xtable partition by hash(col1, col2, col3) 5
The algorithm parameter is not case-sensitive; “md5,” “Md5” and “MD5” are all equivalent. However, if the expression is specified as a character datatype, the value is case sensitive. “Time,” “TIME,” and “time” produce different hash values.
In the absence of a using clause, the bytes that form expression are fed into the hash algorithm in the order they appear in memory. For many datatypes, order is significant. For example, the binary representation of the 4-byte INT value 1 will be 0x00, 0x00, 0x00, 0x01, on MSB-first (big-endian) platforms and 0x01, 0x00, 0x00, 0x00 on LSB-first (little-endian) platforms. Because the stream of bytes is different for different platforms, the hash value is different as well.
With the using clause, the bytes that form expression can be fed into the hashing algorithm in a platform-independent manner. The using clause can also be used to transform character data into Unicode so that the hash value becomes independent of the server’s character configuration.
SQL92- and SQL99-compliant
Any user can execute hashbyte.