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:

http://www.webopedia.com/TERM/L/Luhn_formula.html

http://www.beachnet.com/~hstiles/cardtype.html

http://euro.ecom.cmu.edu/resources/elibrary/everycc.htm