Entering in long sequences of numbers can be fraught with error, say as in a sixteen-digit credit card number or a twelve-digit social security card number. A scientist at IBM in the middle of the last century came up with a formula specifically designed to avoid errors in long numbers called the Luhn Formula or Luhn Algorithm. It works by adding the individual numbers in a specific fashion and resolving that addition to a single digit that is then appended to the end of the number.
By checking the last number against the preceding number using the Luhn formula, the computer can tell the operator if it is a valid number or not. There is a 10 per cent chance that an error might pass the test, but it is a low error margin. This short article is intended to provide a solution written in T-SQL for calculating that same number. For more of a background on Hans Peter Luhn’s work and pattern, see Luhn algorithm - Wikipedia.
After the operator enters in the number, the computer applies the formula against all but the last digit, and if the derived number doesn’t match the last digit, a data entry error occurred. The trick to getting a single digit is to use the modulus function (which T-SQL provides in the form of a percent sign - %) with ten (10) as the operator.
Here is that function:
CREATE FUNCTION dbo.fn_CalcCheckSum(@nNumber2Sum AS INT, @bReturnWithDigit AS BIT)
The function assumes you’ve already stripped the last digit of the number being passed in. It can used to return just the check digit as follows:
Or if you want to generate the number with the check digit appended, this is how it would be called as follows:
The function can used to check the check digit or generate the converted number with the appended check digit for storage in the related table.
Using this function at the SQL Server level, while easy enough, is not recommended for data entry validation as it would be better implement at the application or website level. Wherever feasible, the workload on the SQL Server should be minimized and performed on other tiers of an application. This T-SQL function is best utilized when creating a number to store to the SQL database, or if not stored, when selecting a data set from database where the checksum digit is to be included, such as for generating invoices or receipts.
At MAI, over the years, this checksum digit function has been written in VB, C# and even Visual FoxPro, generally just to return a true or false with the complete string, including the checksum digit, passed into it. As many publishers and associations, our main client base, have moved to online processing by the subscriber and/or member directly, the need for this this of functionality has been reduced. It is still useful in specific cases but only if correctly employed.
If you have questions about checksum digit calculations, please use our contact page to submit them.