MySQL CRC32() Function – Examples

In MySQL, the CRC32() function computes a cyclic redundancy check value and returns a 32-bit unsigned value.

CRC stands for Cyclic Redundancy Check. A CRC is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data (although, it doesn’t necessarily guard against malicious or intentional changes).

Syntax

The syntax goes like this:

CRC32(expr)

Where expr is a string. If the argument is not a string, MySQL treats it as one anyway (either that, or you’ll get an error). If the argument is NULL, then NULL is returned.

Example 1 – Basic Usage

Here’s a basic example to demonstrate what CRC32() returns for a sample string.

SELECT CRC32('Bob');

Result:

+--------------+
| CRC32('Bob') |
+--------------+
|   3448174496 |
+--------------+

Example 2 – Case Sensitivity

You’ll get a different result, depending on the case you use.

SELECT 
  CRC32('Bob'),
  CRC32('bob'),
  CRC32('BOB');

Result:

+--------------+--------------+--------------+
| CRC32('Bob') | CRC32('bob') | CRC32('BOB') |
+--------------+--------------+--------------+
|   3448174496 |   4123767104 |   1668084682 |
+--------------+--------------+--------------+

Example 3 – Numbers

As mentioned, the argument is treated as a string even if it’s not actually a string. Here’s an example where I call the function twice. The first time I call it I pass in a number (123), and the second time I pass in a string ('123').

SELECT 
  CRC32(123),
  CRC32('123');

Result:

+------------+--------------+
| CRC32(123) | CRC32('123') |
+------------+--------------+
| 2286445522 |   2286445522 |
+------------+--------------+

As you can see, we get the same result for both values.

Example 4 – NULL Values

Null values return NULL.

SELECT CRC32(NULL);

Result:

+-------------+
| CRC32(NULL) |
+-------------+
|        NULL |
+-------------+