Stay organized with collections
Save and categorize content based on your preferences.
Spanner supports the following MySQL encryption and compression
functions. You need to implement the MySQL functions in your
Spanner database before you can use them. For more information on
installing the functions, see Install MySQL functions.
bytes_expression: The BYTES value for which to calculate the checksum.
hash_length: The chosen bit length of the hash. Supported values are 256
and 512.
Return data type
STRING
Differences from MySQL
The MySQL version of SHA2() also supports SHA-224 and SHA-384 hash lengths,
which this function does not. Additionally, this function returns the checksum as a
hexadecimal STRING, while the MySQL version returns binary data.
Limitations
This function only supports hash lengths of 256 and 512 bits. The output is
always a hexadecimal string, not binary data. If you provide an unsupported
hash_length, the function returns an error.
Example
The following example calculates the SHA-256 checksum for a BYTES value:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Encryption and compression MySQL functions\n\nSpanner supports the following MySQL encryption and compression\nfunctions. You need to implement the MySQL functions in your\nSpanner database before you can use them. For more information on\ninstalling the functions, see [Install MySQL functions](/spanner/docs/install-mysql-functions).\n\nFunction list\n-------------\n\n`mysql.SHA2`\n------------\n\n mysql.SHA2(bytes_expression, hash_length)\n\n**Description**\n\nCalculates an SHA-2 checksum for the input `BYTES`.\n\nThis function supports the following arguments:\n\n- `bytes_expression`: The `BYTES` value for which to calculate the checksum.\n- `hash_length`: The chosen bit length of the hash. Supported values are 256 and 512.\n\n**Return data type**\n\n`STRING`\n\n**Differences from MySQL**\n\nThe MySQL version of `SHA2()` also supports SHA-224 and SHA-384 hash lengths,\nwhich this function does not. Additionally, this function returns the checksum as a\nhexadecimal `STRING`, while the MySQL version returns binary data.\n\n**Limitations**\n\nThis function only supports hash lengths of 256 and 512 bits. The output is\nalways a hexadecimal string, not binary data. If you provide an unsupported\n`hash_length`, the function returns an error.\n\n**Example**\n\nThe following example calculates the SHA-256 checksum for a `BYTES` value: \n\n SELECT mysql.SHA2(B'GoogleCloud', 256) as sha256_checksum;\n\n /*\n +------------------------------------------------------------------+\n | sha256_checksum |\n +------------------------------------------------------------------+\n | 2002A3F1F350598F5FAF799AD9C9936908230796712E24682016E5257A4D2000 |\n +------------------------------------------------------------------+\n */"]]