Calculate MD5 , SHA256, SHA512 in SQL Server
SQL Server has built-in function HASHBYTES that can be used to calculate hash values. The supported hash algorithms include MD2, MD4, MD5, SHA, SHA1, SHA2_256 and SHA2_512.
Function HASHBYTES
The signature of this function is:
HASHBYTES ( '<algorithm>', { @input | 'input' } ) <algorithm>::= MD2 | MD4 | MD5 | SHA | SHA1 | SHA2_256 | SHA2_512
Examples
MD5
SELECT HASHBYTES('MD5','abc');
Output:
0x900150983CD24FB0D6963F7D28E17F72
SHA256
SELECT HASHBYTES('SHA2_256','abc');
Output:
0xBA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD
SHA512
SELECT HASHBYTES('SHA2_512','abc');
Output:
0xDDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F
Return hexadecimal string
Function HashBytes returns VARBINARY; use CONVERT function to convert the binary to string:
SELECT CONVERT(VARCHAR(100),HASHBYTES('MD5','abc'),1); SELECT CONVERT(VARCHAR(100),HASHBYTES('MD5','abc'),2);
Results:
0x900150983CD24FB0D6963F7D28E17F72
900150983CD24FB0D6963F7D28E17F72
References
copyright
This page is subject to Site terms.
comment Comments
No comments yet.
Log in with external accounts
warning Please login first to view stats information.
article
Spark Read from SQL Server Source using Windows/Kerberos Authentication
article
使用SQL Server Integration Services(SSIS)
article
SQL Server - Using CLR Types and Functions
article
Querying Teradata and SQL Server - Tutorial 1: The SELECT Statement
article
JDBC Integrated Security, NTLM and Kerberos Authentication for SQL Server
Read more (40)