How to use the MySQL CRC32() function

The CRC32() function in MySQL calculates the cyclic redundancy check value of a string. It can be used to generate a checksum value for data validation purposes.

Posted on

The CRC32() function in MySQL calculates the cyclic redundancy check value of a string. It can be used to generate a checksum value for data validation purposes.

Syntax

The syntax for CRC32() is:

CRC32(string)

Where string is the input string to calculate CRC32 checksum for.

Examples

  1. Get CRC32 checksum of a string:

    SELECT CRC32('MySQL Tutorial');
    

    This returns a checksum value of 3259397556.

  2. Calculate CRC32 for a numeric string:

    SELECT CRC32('2534638');
    

    This returns 3888210770.

  3. Get CRC32 checksum for a string with spaces:

    SELECT CRC32('CRC32 Examples');
    

    This returns 966258668.

  4. Calculate CRC32 checksum for a longer string:

    SELECT CRC32('This is an example string to demonstrate CRC32');
    

    This returns 1093078499.

  5. Generate CRC32 checksum from concatenating strings:

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

    This returns 2338997927.

Other Similar Functions

Other checksum functions in MySQL:

  • MD5() - Calculate MD5 checksum
  • SHA1() - Calculate SHA1 checksum
  • SHA2() - Calculate SHA2 checksum
  • CHECKSUM() - Calculate live checksum

So CRC32() provides a way to easily generate a cyclic redundancy checksum for data validation in MySQL.