Sometimes you may need to calculate a CRC on a database column. How can it be done directly from SQL ?

1 ifx_checksum

ifx_checksum is UDR routine declared as:

Copy
create dba function informix.ifx_checksum(informix.bigint, informix.integer)
    returns informix.integer
    with (NOT VARIANT, HANDLESNULLS, PARALLELIZABLE)
    external name '(bigint_checksum)'
    language C;

It's used by the cdr utility as part of the check/repair, so it's got a lot of usage.

It works on all basic data types although, you may need to cast character types to LVARCHAR.... i.e. colname::LVARCHAR...

  • The first parameters is the column od data to checksum.
  • The second parameter is so that we could generate a single checksum on multiple columns.

That way the output of one execution of the ifx_checksum() routine could also be the input of the next. Sort of like chaining the columns together. The innermost call to ifx_checksum needs to have some value as the input value for the second parameter, and you should use zero.

1.1 Multiple hashing

You can nest the calls so that you can generate a single CRC/hash value for multiple columns. As an example suppose that you wanted to hash on three columns, col1, col2, and col3. Then you could write:

Copy
SELECT ifx_checksum(col1, ifx_checksum(col2, ifx_checksum(col3, 0)))

You need ifx_checksum(col3, 0) as the inner most function. The zero value is just a place holder to start the function.

1.2 Hashing by range

As an example suppose that you wanted to limit the hash values between 0 and 1000. You can simply do:

Copy
select mod(abs(ifx_checksum(colname, 0)),1000) from syscolumns

2 MD5

But what if you need a MD5 or other hash function. You can easily extend database functions by adding a C or Java UDR.