MySQL - CRC32() Function
Cyclic Redundancy Check (CRC) is a block code that was invented by W. Wesley Peterson in 1961. It is commonly used to detect accidental changes to data transmitted via telecommunications networks and storage devices.
MySQL provides a set of functions to perform various numerical functions. The CRC32() function calculates the cyclic redundancy check value for a given value and returns it as a 32-bit unsigned value.
Syntax
Following is the syntax of CRC32() function in MySQL −
SELECT CRC32('sample text');
Parameters
This function takes a string value or a numeric value as a parameter.
Return Value
This function returns the 32-bit unsigned value (checksum) of the given value.
Example
The following example uses the CRC32() function to calculate the cyclic redundancy check (CRC) for the given input string −
SELECT CRC32('Test abc 123 **') As Result;
Output
This will produce the following result −
| Result |
|---|
| 3658167795 |
Example
Apart from strings, you can also pass an integer value as a parameter to this function −
SELECT CRC32(12332) As Result;
Output
The output is displayed as below −
| Result |
|---|
| 449853816 |
Example
The below query calculates the cyclic redundancy check (CRC) value for the string in lowercase −
SELECT CRC32('tutorialspoint') As Result;
The output for the query above is produced as given below −
| Result |
|---|
| 3487157699 |
Here, we are calculating the CRC value for string in uppercase −
SELECT CRC32('TUTORIALSPOINT') As Result;
As checksum value is case-sensitive, the checksums for "tutorialspoint" and "TUTORIALSPOINT" will be different due to the difference in letter case.
| Result |
|---|
| 3487157699 |
Example
If you pass NULL as an argument to this function, it returns the same −
SELECT CRC32(NULL) As Result;
Output
The output will be displayed as below −
| Result |
|---|
| 3487157699 |