Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All



Applies To: SQL Server 2014, SQL Server 2016 Preview

Returns the MD2, MD4, MD5, SHA, SHA1, or SHA2 hash of its input in SQL Server.

Applies to: SQL Server (SQL Server 2008 through current version) , Azure SQL Data Warehouse Public Preview.

Topic link icon Transact-SQL Syntax Conventions

HASHBYTES ( '<algorithm>', { @input | 'input' } )<algorithm>::= MD2 | MD4 | MD5 | SHA | SHA1 | SHA2_256 | SHA2_512 


Identifies the hashing algorithm to be used to hash the input. This is a required argument with no default. The single quotation marks are required.


Specifies a variable containing the data to be hashed. @input is varchar, nvarchar, or varbinary.


Specifies an expression that evaluates to a character or binary string to be hashed.

Allowed input values are limited to 8000 bytes. The output conforms to the algorithm standard: 128 bits (16 bytes) for MD2, MD4, and MD5; 160 bits (20 bytes) for SHA and SHA1; 256 bits (32 bytes) for SHA2_256, and 512 bits (64 bytes) for SHA2_512.

256 bits (32 bytes) for SHA2_256, and 512 bits (64 bytes) for SHA2_512 applies to SQL Server 2012 through SQL Server 2016.

varbinary (maximum 8000 bytes)

The following example returns the SHA1 hash of the nvarchar data stored in variable @HashThis.

DECLARE @HashThis nvarchar(4000);
SET @HashThis = CONVERT(nvarchar(4000),'dslfdkjLK85kldhnv$n000#knf');

The following example returns the SHA1 hash of the values in column c1 in the table Test1.

CREATE TABLE dbo.Test1 (c1 nvarchar(50));
INSERT dbo.Test1 VALUES ('This is a test.'), ('This is test 2.');

Here is the result set.


(2 row(s) affected)

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft