SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Add Check Digit (LUHN)

By Ed Klichinsky,

If you ever need to create a credit card like number this stored procedure is for you.

Most credit cards contain a check digit, which is the digit at the end of the credit card number.

To generate the check digit, the LUHN formula is applied to the number. To validate the credit-card number, the check digit is figured into the formula.

Here's how the algorithm works for verifying credit cards; the math is quite simple:

1) Starting with the second to last digit and moving left, double the value of all the alternating digits.

2) Starting from the left, take all the unaffected digits and add them to the results of all the individual digits from step 1. If the results from any of the numbers from step 1 are double digits, make sure to add the two numbers first (i.e. 18 would yield 1+8). Basically, your equation will look like a regular addition problem that adds every single digit.

3) The total from step 2 must end in zero for the credit-card number to be valid.

Additional resources:

Total article views: 440 | Views in the last 30 days: 1
Related Articles

Credit Card Number Validation Escalar Function

Evaluate credit card numbers based on ISO 2894 algorithm.


How to calculate the check digit for a UCC/EAN code

Function that implements the algorithm for calculating the check digit for a UCC/EAN code


mod 10 weights 1, 2

check digit calcualtion


How to add "dot" after every three digits in a number in sql 2005

How to add "dot" after every three digits in a number in sql 2005