HASH

functionsupportedCore SQL functions

docs.snowflake.com ↗

Hash functions

Returns a signed 64-bit hash value. Note that HASH never returns NULL, even for NULL inputs. Possible uses for the HASH function include: - Convert skewed data values to values that are likely to be more randomly or more evenly distributed. For example, you can hash a group of highly skewed values and generate a set of values that are more likely to be randomly distributed or evenly distributed. - Put data in buckets. Because hashing can convert skewed data values to closer-to-evenly distributed values, you can use hashing to help take skewed values and create approximately evenly-sized buckets. If hashing alone is not sufficient to get the number of distinct buckets that you want, you can combine hashing with the ROUND or WIDTH_BUCKET functions. Note HASH is a proprietary function that accepts a variable number of input expressions of arbitrary types and returns a signed value. It is not a cryptographic hash function and should not be used as such. Cryptographic hash functions have a few properties which this function does not, for example: - The cryptographic hashing of a value cannot be inverted to find the original value. - Given a value, it is infeasible to find another value with the same cryptographic hash. For cryptographic purposes, use the SHA families of functions (in String & binary functions). See also: HASH_AGG

Syntax

HASH( <expr> [ , <expr> ... ] )

HASH(*)

Parameters

NameRequiredDescription
exprrequiredThe expression can be a general expression of any Snowflake data type.
*requiredReturns a single hashed value based on all columns in each record, including records with NULL values. When you pass a wildcard to the function, you can qualify the wildcard with the name or alias for the table. For example, to pass in all of the columns from the table named mytable, specify the following: Copy code (mytable.*) You can also use the ILIKE and EXCLUDE keywords for filtering: ILIKE filters for column names that match the specified pattern. Only one pattern is allowed. For example: Copy code (* ILIKE 'col1%') EXCLUDE filters out column names that don’t match the specified column or columns. For example: Copy code (* EXCLUDE col1) (* EXCLUDE (col1, col2)) Qualifiers are valid when you use these keywords. The following example uses the ILIKE keyword to filter for all of the columns that match the pattern col1% in the table mytable: Copy code (mytable.* ILIKE 'col1%') The ILIKE and EXCLUDE keywords can’t be combined in a single function call. For this function, the ILIKE and EXCLUDE keywords are valid only in a SELECT list or GROUP BY clause. For more information about the ILIKE and EXCLUDE keywords, see the “Parameters” section in SELECT.

Returns

Returns a signed 64-bit value as NUMBER(19,0). HASH never returns NULL, even for NULL inputs.

Usage notes

Collation details

No impact. - Two strings that are identical but have different collation specifications have the same hash value. In other words, only the string, not the collation specification, affects the hash value. - Two strings that are different, but compare equal according to a collation, might have a different hash value. For example, two strings that are identical using punctuation-insensitive collation will normally have different hash values because only the string, not the collation specification, affects the hash value.