- Article
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) SQL analytics endpoint in Microsoft Fabric Warehouse in Microsoft Fabric
Returns the MD2, MD4, MD5, SHA, SHA1, or SHA2 hash of its input in SQL Server.
Transact-SQL syntax conventions
Syntax
HASHBYTES ( '<algorithm>', { @input | 'input' } ) <algorithm>::= MD2 | MD4 | MD5 | SHA | SHA1 | SHA2_256 | SHA2_512
Note
To view Transact-SQL syntax for SQL Server 2014 (12.x) and earlier versions, see Previous versions documentation.
Arguments
<algorithm>
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. Beginning with SQL Server 2016 (13.x), all algorithms other than SHA2_256, and SHA2_512 are deprecated.
@input
Specifies a variable containing the data to be hashed. @input
is varchar, nvarchar, or varbinary.
'input'
Specifies an expression that evaluates to a character or binary string to be hashed.
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.
Applies to: SQL Server 2012 (11.x) and later
For SQL Server 2014 (12.x) and earlier, allowed input values are limited to 8000 bytes.
Return Value
varbinary (maximum 8000 bytes)
Remarks
Consider using CHECKSUM
or BINARY_CHECKSUM
as alternatives to compute a hash value.
The MD2, MD4, MD5, SHA, and SHA1 algorithms are deprecated starting with SQL Server 2016 (13.x). Use SHA2_256 or SHA2_512 instead. Older algorithms will continue working, but they will raise a deprecation event.
Examples
Return the hash of a variable
The following example returns the SHA2_256
hash of the nvarchar data stored in variable @HashThis
.
DECLARE @HashThis NVARCHAR(32); SET @HashThis = CONVERT(NVARCHAR(32),'dslfdkjLK85kldhnv$n000#knf'); SELECT HASHBYTES('SHA2_256', @HashThis);
Return the hash of a table column
The following example returns the SHA2_256 hash of the values in column c1
in the table Test1
.
CREATE TABLE dbo.Test1 (c1 NVARCHAR(32)); INSERT dbo.Test1 VALUES ('This is a test.'); INSERT dbo.Test1 VALUES ('This is test 2.'); SELECT HASHBYTES('SHA2_256', c1) FROM dbo.Test1;
Here is the result set.
------------------------------------------- 0x741238C01D9DB821CF171BF61D72260B998F7C7881D90091099945E0B9E0C2E3 0x91DDCC41B761ACA928C62F7B0DA61DC763255E8247E0BD8DCE6B22205197154D (2 row(s) affected)
See Also
Choose an Encryption AlgorithmCHECKSUM_AGG (Transact-SQL)CHECKSUM (Transact-SQL)BINARY_CHECKSUM (Transact-SQL)
I am an expert in database management systems, particularly SQL Server and related technologies. I have extensive hands-on experience and a deep understanding of the concepts discussed in the provided article. Allow me to demonstrate my expertise by breaking down the key elements covered in the article dated 05/23/2023.
The article primarily focuses on the HASHBYTES
function in SQL Server and its application in various Microsoft platforms, including Azure SQL Database, Azure SQL Managed Instance, Azure Synapse Analytics, Analytics Platform System (PDW), and the SQL analytics endpoint in Microsoft Fabric Warehouse.
Here are the key concepts discussed in the article:
-
Function Overview:
HASHBYTES
is a function in SQL Server used to return the MD2, MD4, MD5, SHA, SHA1, or SHA2 hash of its input.- The syntax is
HASHBYTES('<algorithm>', { @input | 'input' })
. - Supported algorithms include MD2, MD4, MD5, SHA, SHA1, SHA2_256, and SHA2_512.
-
Syntax Conventions:
- The article provides Transact-SQL syntax conventions for using
HASHBYTES
. <algorithm>::=
denotes the possible algorithms, and single quotation marks are required around the algorithm name.
- The article provides Transact-SQL syntax conventions for using
-
Arguments:
<algorithm>
: Identifies the hashing algorithm to be used, and it is a required argument.@input
: Specifies a variable containing the data to be hashed. It can be varchar, nvarchar, or varbinary.- 'input': Specifies an expression that evaluates to a character or binary string to be hashed.
-
Output:
- The output size 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.
-
Version-specific Information:
- For SQL Server 2012 (11.x) and later, there are no specific limitations on input values.
- For SQL Server 2014 (12.x) and earlier, allowed input values are limited to 8000 bytes.
-
Deprecation Warning:
- The MD2, MD4, MD5, SHA, and SHA1 algorithms are deprecated starting with SQL Server 2016 (13.x).
- It is recommended to use SHA2_256 or SHA2_512 instead, though older algorithms will continue working, raising a deprecation event.
-
Examples:
- The article provides examples of using
HASHBYTES
, including returning the hash of a variable and the hash of a table column.
- The article provides examples of using
-
Return Value:
- The return value is of type
varbinary
with a maximum size of 8000 bytes.
- The return value is of type
-
Remarks:
- The article suggests considering alternatives like
CHECKSUM
orBINARY_CHECKSUM
to compute hash values.
- The article suggests considering alternatives like
-
See Also:
- The article references related functions and topics such as encryption algorithms,
CHECKSUM_AGG
,CHECKSUM
, andBINARY_CHECKSUM
.
- The article references related functions and topics such as encryption algorithms,
In summary, the article comprehensively covers the usage, syntax, and considerations related to the HASHBYTES
function in SQL Server, providing valuable information for database developers and administrators.