How to get a cyclic redundancy check i.e CRC32() value in MySQL8 ?

With MySQL, we can do complex calculations very easily with the help of inbuilt mathematical functions. CRC32(str) helps in calculating the cyclic redundancy check value and returns a 32-bit unsigned value.

Now let’s start with it.


CRC32(str) is a mathematical function. CRC means cyclic redundancy check.

cyclic redundancy check (CRC) is an error-detecting code commonly used in digital networks and storage devices to detect accidental changes to raw data.

Computes a cyclic redundancy check value and returns a 32-bit unsigned value. The argument is expected to be a string and (if possible) is treated as one if it is not.

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

It returns the CRC of input string str. Where str is the argument, whose value will be passed by the user.

It will return NULL, if str is passed as NULL.


MySQL CRC32() : Syntax

CRC32 ( X );

MySQL CRC32() : Parameter

Name, Required /Optional, Description
str , Required, A string whose CRC32 value is to be fetched.


MySQL CRC32() : Output

Return, Description
NULL, if the argument is NULL.
It returns 32-bit unsigned value cyclic redundancy value of input string.


MySQL CRC32()  Available from : MySQL 4.1


CRC32() Example 1 : Basic Examples

See the below example. See the output, when ‘www.tutorialmines.net’ is passed.

mysql> SELECT CRC32('www.tutorialmines.net') AS 'crc value';
+------------+
| crc value |
+------------+
| 2600963911 |
+------------+
1 row in set (0.00 sec)

Now, Let’s change the input value to 56456. See what it returns.

mysql> SELECT CRC32(56456) AS 'crc value';
+-----------+
| crc value |
+-----------+
| 877297141 |
+-----------+
1 row in set (0.00 sec)

CRC32() Example 2 : Using it with PI()

We are using the PI() function to get the value of CRC32() with it. I get the below value.

mysql> SELECT CRC32(PI()) AS 'crc value';
+------------+
| crc value |
+------------+
| 2969982827 |
+------------+
1 row in set (0.00 sec)

CRC32() Example 3 : Case Sensitivity

Here you can see the different results on the basis of the case of your arguments.

mysql> SELECT CRC32('TUTORIALMINES'), 
              CRC32('Tutorialmines'), 
              CRC32('TutorialMINES');
+------------------------+------------------------+------------------------+
| CRC32('TUTORIALMINES') | CRC32('Tutorialmines') | CRC32('TutorialMINES') |
+------------------------+------------------------+------------------------+
| 1993232533             | 3824949637             | 346610485 |
+------------------------+------------------------+------------------------+
1 row in set (0.00 sec)

CRC32() Example 4 : NULL arguments

If the argument is NULL, it will return NULL. See the below example :

mysql> SELECT CRC32(NULL) AS 'crc value';
+-----------+
| crc value |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)

Related articles : PI(), COS(), MySQL Maths.

You may also like...