Generating Fake Credit Card Numbers in SQL Server with the Luhn Algorithm (for Test Data)

Comments 0

Share to social media

For development and test databases that include payment-card data, using real card numbers is a compliance nightmare (PCI DSS) and a liability. The alternative is generating fake credit card numbers that look realistic – correct length for the brand, valid-looking prefix ranges, and passing the Luhn checksum that payment systems use as a first-pass validity check. This article shows how to generate Luhn-valid fake card numbers in SQL Server: implementing the Luhn algorithm as a T-SQL function, using brand-appropriate prefix ranges (Visa starts with 4, Mastercard with 5 or 2, Amex with 34 or 37, Discover with 6011 or 65), and a BigRandomNumberView for producing batches of cards at once. (The generated numbers are unambiguously fake (don’t match real issuing bank BINs) but will survive input validation in test scenarios that check only format and checksum.) I haven’t seen a SQL Server table with real unencrypted credit card numbers for several years, and I don’t know of any good reasons to have them stored that way. However, I’ve needed them in the past for testing a web application that had to take credit card details. Generating credit cards in a way that conforms to a particular distribution is reasonably easy in SQL Server, though. The only difficult bit is the fact that there is a validation checksum. Otherwise it is a good example of how to solve the problem of spoofing the more specialist types of data. Imagine that we need to create fake credit cards from a fairly large table. We don’t want, and can’t get, the real information, but our fake credit cards have to conform to the distribution of the real data. All we need is an aggregation of the real data that tells us the distribution. Fortunately, the first six digits of a credit Card has only the details of the bank, currency and so on. We aren’t going to associate the fake data with an identifiable person. We are pretty safe. We are just generating a list of credit cards. We will draw from the list at random and not associate the randomly-generated suffix with the original person. We start with the real data. OK, it isn’t real data, but we’ll pretend that it is. The data I used is provided with this blog so you can try it out. It conforms roughly with the proportion of providers that you’ll see in the States. … A lot of rows omitted  here … We just need to know the distribution. You must imagine that these cards are from a production system that you will never see. In reality, you would just receive from the Ops people the Generation Table #GenerationTable so you wouldn’t need access to the actual data Now we can use the Generation Table to provide valid credit cards with the correct distribution. Note that, by this method, you will only get credit card numbers with the prefixes that come from the generation table. With this table we can quickly throw together a routine that provides our friends in AdventureWorks (or your spoof customer names) with credit cards. We use the ‘zip’technique. To do this, you would need to start with a view that gives you a twenty digit number when combined with the six-digit prefix Now, we need the function that produces the spoof credit card that is correct according to Luhn’s algorithm And finally, you have a view that contains just the number of random numbers that you need. I’ve bulked this up to 4000 just to test out and make sure the routine performed well. The source is with the blog. … and so on down to the end … (it avoids iteration and is fast) We can now compare the distribution of the old and new data, using the routine published here in ‘Visual Checks on How Data is Distributed in SQL Server’ Can we improve on this in order to get a smoother distribution of provider strings? At the moment we can’t create a six-digit provider string that isn’t in the original data. To do so isn’t easy, because each provider uses the string in a different way, and not every combination of digit is used even for the individual provider. It would require maintenance as well because provider strings can come and so, or change. This following routine is designed for generating lists. You can check the routine that creates the card by using an existing routine, dbo.fnIsValidCard, that I wrote a while back in The Luhn Algorithm in SQL .

Downloads

BigRandomNumberView SampleCreditCards

FAQs

1. How do I generate fake credit card numbers in SQL Server for test data?

Three elements make a fake card number look realistic: correct length (13-19 digits depending on brand), valid-looking prefix (4 for Visa, 5 or 2 for Mastercard, 34 or 37 for Amex, 6011 or 65 for Discover), and a valid Luhn checksum (the last digit chosen so the whole number passes the Luhn algorithm). In SQL Server, implement the Luhn algorithm as a scalar function, generate random digits for all positions except the last, compute the Luhn-valid final digit, and prepend a brand prefix. Combine with a BigRandomNumberView (as shown in the article) to produce many cards at once. The resulting numbers pass basic validation but don’t correspond to real issuing-bank BINs.

2. What is the Luhn algorithm?

The Luhn algorithm is a simple checksum used to validate credit card numbers, IMEI numbers, national identification numbers, and similar identifiers. For a number, starting from the rightmost digit: double every second digit; if doubling produces a two-digit result, sum its digits; sum all the results; the total modulo 10 must equal zero. For a number being generated, compute the same sum for all digits except the last, then set the last digit so the total is congruent to zero modulo 10. The algorithm catches most single-digit errors and many transposition errors – it’s the ‘first line of defence’ against typos in card number entry, not a security measure.

Generating Luhn-valid fake card numbers for use in test environments is legitimate and common practice – payment processors and major card brands publish test card numbers specifically for this purpose (Visa 4111 1111 1111 1111, Mastercard 5555 5555 5555 4444, etc.). Using those published test numbers is the safest approach for integration testing. Generating your own fake numbers is fine for populating test databases where you just need plausible-looking data that passes format validation; the numbers should not match real BIN prefixes of active issuing banks, should not be used to attempt actual transactions (which would be fraud), and should never appear in production systems where they might be mistaken for real cards.

4. What are the brand-specific credit card number formats?

The major brands have distinguishing prefix ranges and lengths: Visa – 16 digits starting with 4 (also 13 and 19 in rare cases); Mastercard – 16 digits starting with 51-55 (or 2221-2720 in newer ranges); American Express – 15 digits starting with 34 or 37; Discover – 16 digits starting with 6011 or 65, or 14-19 digits in newer ranges; JCB – 16 digits starting with 3528-3589; Diners Club – 14 digits starting with 300-305, 36, or 38. All use the Luhn algorithm for the final-digit checksum. For test data generation, pick the brand and length you need, generate random digits within the brand’s prefix and length, and Luhn-compute the final digit.

This document contains proprietary information and is protected by copyright law.

Copyright © 2026 Red Gate Software Limited. All rights reserved

Article tags

About the author

Phil Factor

See Profile

Phil Factor (real name withheld to protect the guilty), aka Database Mole, has 40 years of experience with database-intensive applications. Despite having once been shouted at by a furious Bill Gates at an exhibition in the early 1980s, he has remained resolutely anonymous throughout his career. See also :