HASHBYTES (Transact-SQL) - SQL Server (2024)

  • Article

Applies to: HASHBYTES (Transact-SQL) - SQL Server (1) SQL Server HASHBYTES (Transact-SQL) - SQL Server (2) Azure SQL Database HASHBYTES (Transact-SQL) - SQL Server (3) Azure SQL Managed Instance HASHBYTES (Transact-SQL) - SQL Server (4) Azure Synapse Analytics HASHBYTES (Transact-SQL) - SQL Server (5) Analytics Platform System (PDW) HASHBYTES (Transact-SQL) - SQL Server (6) SQL analytics endpoint in Microsoft Fabric HASHBYTES (Transact-SQL) - SQL Server (7) Warehouse in Microsoft Fabric

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

HASHBYTES (Transact-SQL) - SQL Server (8) 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:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. Examples:

    • The article provides examples of using HASHBYTES, including returning the hash of a variable and the hash of a table column.
  8. Return Value:

    • The return value is of type varbinary with a maximum size of 8000 bytes.
  9. Remarks:

    • The article suggests considering alternatives like CHECKSUM or BINARY_CHECKSUM to compute hash values.
  10. See Also:

    • The article references related functions and topics such as encryption algorithms, CHECKSUM_AGG, CHECKSUM, and BINARY_CHECKSUM.

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.

HASHBYTES (Transact-SQL) - SQL Server (2024)

FAQs

What is the use of hashbytes in SQL Server? ›

hash function - A hash function takes in data and returns back a fixed length block of bits such that any change to the data should result in a different block. HASHBYTES() is actually a function which provides access to several hashing algorithms.

What is the alternative to Hashbytes in SQL Server? ›

Consider using CHECKSUM or BINARY_CHECKSUM as alternatives to compute a hash value.

How to decrypt hashbytes in SQL Server? ›

You can't - that's the whole idea. A Hash code (like MD5 or SHA) is not encryption: it can't be reversed: Decrypting MD5 and SHA: Why You Can't Do It[^] - this applies to all hashing algorithms.

What is the maximum length of Hashbytes in SQL Server? ›

According to the documentation HASHBYTES RETURN VALUE varbinary (maximum 8000 bytes).

What is the difference between checksum and Hashbytes in SQL? ›

CHECKSUM computes a 32-bit integer value by quite simple-minded XOR-algorithm. hashbytes uses a more sophisticated algorithm, and the value is longer. If you want a unique hash value to use for comparison rather than comparing the base values, you cannot use checksum, because the risk for collisions is too big..

Why do we use hash in SQL? ›

Hashing is commonly used to ensure data integrity. By generating a hash value for an amount of data, such as a file or message, a user can later compare it with the hash value of the received data to verify if any changes or corruption occurred during transmission. Efficient data retrieval.

Is Hashbytes reversible? ›

In short, hashing is the process of using an algorithm to transform large quantities of data into a fixed value. This can be achieved with any volume of data, and the output data size can be any length. This is called a hash value – the hash value is fixed and non-reversible.

How to encrypt data in SQL Server? ›

Applies to: SQL Server.
  1. Create a master key.
  2. Create or obtain a certificate protected by the master key.
  3. Create a database encryption key and protect it by using the certificate.
  4. Set the database to use encryption.
Jan 19, 2024

What is the difference between binary checksum and checksum? ›

This should work with all types and any combinations of types. The difference between BINARY_CHECKSUM an CHECKSUM is that CHECKSUM(a)=CHECKSUM(b) if a=b (or both are NULLs) - so for strings it's charset and collation issue. As manual says - CHECKSUM is intented for building hash indexes.

What is hashkey in SQL? ›

A hash key is the output from a hashing algorithm, where a specific input value is transformed into a distinct, unique string per input value. The purpose of hash keys in this context is to provide a surrogate key for business keys, composite business keys and business key combinations.

Can hash key be decrypted? ›

No, hashed data cannot be decrypted. Hashing is a one-way process, which means that the original data cannot be obtained from the hash value. It is designed to be irreversible, making it suitable for storing passwords and verifying data integrity.

What is the MD5 algorithm in SQL Server? ›

The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. The value returned by the MD5 function is a binary string of 32 hexadecimal digits, or NULL if the argument was NULL.

What are the different types of Hashbytes in SQL? ›

The possible values are MD2 , MD4 , MD5 , SHA , SHA1 , SHA2_256 , and SHA2_512 , but only the last two are recommended in SQL Server 2017. input : This is an input variable, column, or expression that needs to be hashed. The data types that are allowed are varchar , nvarchar , and varbinary .

What is the meaning of Hashbytes? ›

The SQL Server HASHBYTES function is a powerful tool for hashing values using various algorithms like MD5 and SHA. Its significance lies in ensuring data integrity and enabling change detection within databases.

What is the largest string in SQL Server? ›

String Data Types
Data typeDescriptionMax size
char(n)Fixed width character string8,000 characters
varchar(n)Variable width character string8,000 characters
varchar(max)Variable width character string1,073,741,824 characters
textVariable width character string2GB of text data
8 more rows

When to use hash index SQL Server? ›

A HASH INDEX is most effective when the data distribution is uniform, and you need to perform a lot of equality comparisons. However, if your queries involve range-based searches or sorting operations, a B-Tree index might be a better choice.

What is the purpose of hash order group in SQL? ›

1 Answer. Oracle's SQL Tuning Guide says in the PLAN_TABLE reference: HASH GROUP BY: Operation hashing a set of rows into groups for a query with a GROUP BY clause. SORT GROUP BY: Operation sorting a set of rows into groups for a query with a GROUP BY clause.

Why use hash keys? ›

They can be used to offer significant performance improvements within data warehousing scenarios, while also offer robust repeatability and consistency in outputs of surrogate key generation, simplifying data migrations and data processing workloads.

What is the SHA256 function in SQL Server? ›

A function used to calculate the SHA-256 hash of a data value. The hash will be returned as a hex-encoded string.

Top Articles
Should you reinvest company profits back into your business? - Haines Watts Group
Carriage of Goods by Sea Act of 1936 (COGSA)
Automated refuse, recycling for most residences; schedule announced | Lehigh Valley Press
Compare Foods Wilson Nc
Bj 사슴이 분수
Paris 2024: Kellie Harrington has 'no more mountains' as double Olympic champion retires
Wellcare Dual Align 129 (HMO D-SNP) - Hearing Aid Benefits | FreeHearingTest.org
What are Dietary Reference Intakes?
Gabrielle Abbate Obituary
Costco The Dalles Or
About Goodwill – Goodwill NY/NJ
Otr Cross Reference
Simple Steamed Purple Sweet Potatoes
Methodist Laborworkx
Sarpian Cat
Shreveport Active 911
Byte Delta Dental
Nutrislice Menus
Uky Linkblue Login
Trac Cbna
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Moving Sales Craigslist
E32 Ultipro Desktop Version
Surplus property Definition: 397 Samples | Law Insider
Stihl Dealer Albuquerque
Prot Pally Wrath Pre Patch
Silky Jet Water Flosser
Finding Safety Data Sheets
800-695-2780
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Was heißt AMK? » Bedeutung und Herkunft des Ausdrucks
60 Second Burger Run Unblocked
Emiri's Adventures
Gwen Stacy Rule 4
Craigslist In Myrtle Beach
Jefferson Parish Dump Wall Blvd
Grapes And Hops Festival Jamestown Ny
Baywatch 2017 123Movies
Studio 22 Nashville Review
Timberwolves Point Guard History
Lamont Mortuary Globe Az
6576771660
The Great Brian Last
Pickwick Electric Power Outage
Bedbathandbeyond Flemington Nj
Autozone Battery Hold Down
Mike De Beer Twitter
Convert Celsius to Kelvin
Provincial Freeman (Toronto and Chatham, ON: Mary Ann Shadd Cary (October 9, 1823 – June 5, 1893)), November 3, 1855, p. 1
All Obituaries | Roberts Funeral Home | Logan OH funeral home and cremation
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 6575

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.