// strings reference

SQL CRC32 function

Computes the 32-bit CRC checksum of a string, returned as an unsigned integer.

Categoria
Strings
Returns
unsigned integer
In this category
127 of 172

Sintaxe

CRC32(string)

Suporte a mecanismos

MotorSupportNotas
SQL Server Not built in No CRC32; CHECKSUM and BINARY_CHECKSUM use other algorithms.
PostgreSQL Sim crc32(bytea) and crc32c(bytea) from PostgreSQL 18; convert text with convert_to.
MySQL Sim Supported under this name.
SQLite Not built in No CRC32 built in.
Oracle Not built in No CRC32; ORA_HASH or STANDARD_HASH.

Exemplos

MySQL

SELECT CRC32('hello') AS checksum;  -- 907060870

MySQL

SELECT * FROM documents
WHERE CRC32(body) <> stored_crc;

PostgreSQL

SELECT crc32(convert_to(body, 'UTF8')) AS checksum
FROM documents;

Bom saber

  • CRC32 detects accidental corruption, not deliberate tampering; it has no cryptographic strength.
  • With 2^32 possible values, collisions become likely after about 77,000 distinct inputs (the birthday bound). Treat equal CRCs as a hint and compare the actual values.
  • The result is an integer, so it indexes and compares cheaply; a classic use is a stored checksum column that flags changed text.

Questions about CRC32

Can CRC32 be used as a unique identifier?

No. Collisions are certain at scale; use it to spot likely changes and confirm with the full value.

What is the difference between CRC32 and CHECKSUM in SQL Server?

Different algorithms: CRC32 is the standard cyclic code over one string; SQL Server's CHECKSUM hashes an expression list. Their values never agree.

Does PostgreSQL have CRC32?

From PostgreSQL 18: crc32(bytea) gives the same value as MySQL's CRC32 for the same bytes (907060870 for 'hello'), and crc32c uses the Castagnoli polynomial.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL, MySQL.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.