MariaDB CRC32() Function

In MariaDB, CRC32() is a built-in numeric function that computes a cyclic redundancy check value and returns a 32-bit unsigned value.

Cyclic Redundancy Check (CRC) is a channel coding technology that generates a short fixed-digit check code based on data such as network packets or computer files. It is mainly used to detect or verify data transmission or storage. Errors that may occur after. It uses the principle of division and remainder for error detection.

MariaDB CRC32() Syntax

Here is the syntax of the MariaDB CRC32() function:

CRC32(str)

or (>= MariaDB 10.8)

CRC32([par,]str)

Parameters

str

Required. A string used for calculation. Non-string arguments will be treated as strings.

par

Optional. One computes the cyclic redundancy check value.

If you provide the wrong number of parameters, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'CRC32'.

Return value

The MariaDB CRC32() function is used to calculate the cyclic redundancy check value and returns a 32-bit unsigned value.

If any parameter is NULL, the CRC32() function will return NULL.

MariaDB CRC32() Examples

Basic example

To calculate the cyclic redundancy check value for Hello, use the following statement:

SELECT CRC32('Hello');

Output:

+----------------+
| CRC32('Hello') |
+----------------+
|     4157704578 |
+----------------+

Case sensitive

The cyclic redundancy check value is different for strings of different case:

SELECT
  CRC32('Hello'),
  CRC32('HELLO');

Output:

+----------------+----------------+
| CRC32('Hello') | CRC32('HELLO') |
+----------------+----------------+
|     4157704578 |     3242484790 |
+----------------+----------------+

Non-string parameter

MariaDB CRC32() will treat non-string arguments as strings:

SELECT
  CRC32('1234'),
  CRC32(1234);

Output:

+---------------+-------------+
| CRC32('1234') | CRC32(1234) |
+---------------+-------------+
|    2615402659 |  2615402659 |
+---------------+-------------+

Segmented computing

Normally, CRC is calculated in pieces. To facilitate this, MariaDB 10.8.0 introduces an optional parameter: CRC32('HelloWorld') = CRC32(CRC32('Hello'),'World'):

SELECT
  CRC32('HelloWorld'),
  CRC32(CRC32('Hello'), 'World');

Output:

+---------------------+--------------------------------+
| CRC32('HelloWorld') | CRC32(CRC32('Hello'), 'World') |
+---------------------+--------------------------------+
|          2004290681 |                     2004290681 |
+---------------------+--------------------------------+

In this example, we split HelloWorld into Hello and World, and calculate them piecewise. You can split it according to your needs, the result is the same, as follows:

SELECT CRC32(CRC32('He'), 'lloWorld');

Output:

+--------------------------------+
| CRC32(CRC32('He'), 'lloWorld') |
+--------------------------------+
|                     2004290681 |
+--------------------------------+

Conclusion

In MariaDB, CRC32() is a built-in numeric function that computes a cyclic redundancy check value and returns a 32-bit unsigned value.