473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Find Credit Card numbers in text field

code green
1,726 Expert 1GB
Data Protection etc.... shouldn't be there.
How do I find credit card numbers buried amongst any length of text.

Googling finds credit card validation claims, which are too complicated.

I need to find groups of numbers that resemble credit card numbers.
I think the criteria is 13 - 24 digits in groups of 4 or 5
seperated by spaces or dashes or bunched together.

It looks like a regex using PATINDEX is the answer but regex is black magic to me
Have I got this right and could I please have some pattern suggestions?
Expand|Select|Wrap|Line Numbers
  1. SELECT notes FROM cust_order WHERE PATINDEX('pattern',notes) > 0
May 19 '09 #1
4 15992
Using a similar function to the one I posted last time you could get something that might help you on your way (I'm not sure what CK had in mind for an alternative approach with String Parsing so I'm bumbling on as before !):

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION [dbo].[R_Test_func2](@sub nvarchar(1000))
  2. RETURNS int
  3. AS
  4. BEGIN
  5.  
  6. DECLARE @Output int
  7. DECLARE @myOutput int
  8. SET @Output = 0
  9. SET @myOutput = 0
  10.  
  11. WHILE patindex('%[0123456789]%', @sub) <> 0
  12.  BEGIN
  13.   SET @Output = 
  14.    len(substring
  15.    (
  16.     substring(@sub,patindex('%[0123456789]%',@sub),len(@sub))
  17.     ,1
  18.     ,CASE
  19.      WHEN patindex('%[^0123456789 -]%',substring(@sub,patindex('%[0123456789]%',@sub),len(@sub))) = 0
  20.      THEN len(@sub)
  21.      ELSE patindex('%[^0123456789 -]%',substring(@sub,patindex('%[0123456789]%',@sub),len(@sub)))-1
  22.     END
  23.    ))
  24.  
  25.   SET @sub = 
  26.    CASE
  27.     WHEN patindex('%[^0123456789 -]%',substring(@sub,patindex('%[0123456789]%',@sub),len(@sub))) = 0
  28.     THEN 'a'
  29.     ELSE substring
  30.     (
  31.      substring(@sub,patindex('%[0123456789]%',@sub),len(@sub))
  32.      ,patindex('%[^0123456789 -]%',substring(@sub,patindex('%[0123456789]%',@sub),len(@sub)))
  33.      ,len(@sub)
  34.     )
  35.    END
  36.  
  37.   SET @myOutput = 
  38.    CASE
  39.     WHEN @Output > @myOutput THEN @Output ELSE @myOutput
  40.     END
  41.   END
  42.  RETURN  @myOutput
  43.  
  44. END
The general principle is the same as before

It looks for the 1st numeric character:
Expand|Select|Wrap|Line Numbers
  1. patindex('%[0123456789]%',@sub)
Then for the 1st non numeric character thereafter (this time also allowing for a space or -):
Expand|Select|Wrap|Line Numbers
  1. patindex('%[^0123456789 -]%',substring(@sub,patindex('%[0123456789]%',@sub),len(@sub)))
The length of this string is stored and then the rest of the string is searched.
The resulting output will be the length of the longest string of consecutive numbers (including spaces and -)

So, you could use a WHERE statement such as:

Expand|Select|Wrap|Line Numbers
  1. WHERE dbo.R_Test_Func2(notes) > 12
You could get rid of one of the two variables @Output and @myOutput by doing the SET as a CASE statement in one go but I felt this may complicate the (already less than easy to read) code too much!

As a note, another fix from the original function I posted is that it will now work if the last character is non-numeric, hence the addition of a couple of CASE statements buried in there. In the previous version if the last character was not a number then it would try and return a string of either length 0 or starting from the 0th character which I discovered it did not like very much!


Hope this helps
May 20 '09 #2
code green
1,726 Expert 1GB
Looks like it could work Uncle Dickie.
I am under a bit of pressure with this one now, so will give it a try
May 20 '09 #3
ck9663
2,878 Expert 2GB
It meant the same thing :) You're basically parsing the TEXT which is by common definition is a string. As string parsing is reading the string one character at a time or finding patterns within it.

As I always said, if that algorithm gave you the performance you need, that should be fine, until someone suggested a better one :)

Thanks Uncle Dickie and Happy Coding CG :)


--- CK
May 20 '09 #4
Having looked a bit more at the patindex function, a much easier option than my previous function would simply be:

Expand|Select|Wrap|Line Numbers
  1. SELECT notes
  2. FROM   cust_order
  3. WHERE  patindex('%[0-9][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -][0-9 -]%',TranID) > 0
  4.  
This is just looking for a string of 13 consecutive numbers, spaces or '-' (it must start with a number).
May 21 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: gl | last post by:
I have just started a project that's going to do very heavy credit card processing through asp.net and i had some questions. I've never really done any cc processing through code and I wasn't sure...
4
by: Michelle A. | last post by:
I have a form that takes in a credit card number, just a series of numbers 1234123412341234. When they get to the "Review" page and display all the information a user has entered, I would like the...
5
by: John | last post by:
Hi, I've always had the opinion that you don't store credit card numbers on a hosted website database. But it has occurred to me, that perhaps I am over reacting, and encrypted CC info may be...
6
by: Arne | last post by:
What would be a good component for processing credit cards? (I am not using commerce server.) Would I need to encrypt the credit card column in the database?
4
by: Jerry Camel | last post by:
I'm writing and ASP.net app using vb .net. I need to interact with a credit card reader. I have one that sits inline with the keyboard. Works great, except for the fact that no matter what field...
12
by: Jerry Camel | last post by:
Not sure if this is a good place to post this... I'm writing and ASP.net app using vb .net. I need to interact with a credit card reader. I have one that sits inline with the keyboard. Works...
1
by: securedcardss | last post by:
http://card.2youtop.info secured credit card card credit instant secured card cash credit secured card
6
by: samatair | last post by:
I need to create a form which accepts credit card numbers and mail the details with the credit card number to my client. I came to know that using SSL would make the form HTTPS and make it secure....
17
by: sharsy | last post by:
Hello guys, I would like some help in generating query criteria that will identify credit cards that have expired on an access database. The specific Field is formatted with a Data Type of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.