Solved

Way to strip out all non-numer characters in Access Query

Posted on 2009-07-01
3
560 Views
Last Modified: 2012-05-07
I have a basic query that is trying to clean up a TIN/SSN field.  right now there are many non-numeric characters that I'd like to clean.  The below works well for mos of the characters, but I also have several cases where the users put in words (e.g. SSN:"123-45-6789").  I'm looking for a quick way to remove ALL non-numeric characters.  

NewTaxID: Replace(Replace(Replace(Replace(Replace([Tax_Id],"-",""),":","")," ",""),"_",""),"/","")
NewTaxID: Replace(Replace(Replace(Replace(Replace([Tax_Id],"-",""),":","")," ",""),"_",""),"/","")

Open in new window

0
Comment
Question by:BBlu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 50 total points
ID: 24759391
Hello BBlu,

Add the code below to a regular VBA module, and then use this in the query designer:

NewTaxID: RegExpReplace([Tax_ID], "[^0-9]", "")

Regards,

Patrick
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True) 
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll controls
    ' whether all instances of the matched string are replaced (True) or just the first instance (False)
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
    
    Set RegX = Nothing
    
End Function

Open in new window

0
 

Author Comment

by:BBlu
ID: 24759408
Wow!  I almost feel guilty that it was that easy!  Now I need to dive into the VBA module to see how/why it works.  Thanks, Patrick!  
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24759522
Glad to help!  BTW, you may want to try the version below.  If you have a lot of records to sort through, it
may run marginally faster.

Regular Expressions is an amazingly flexible and fast way to parse text.  Many people use it in Perl, and it is
also implemented in VBScript.  This UDF instantiates the RegExp class from VBScript and exposes it in
VBA.  I use it, and a similar RegExp-powered "find" function, very frequently.

The pattern string [^0-9] means "any character EXCEPT 0 through 9", and then in the formula I wrote any
such character is replaced with a zero-length string.
Function RegExpReplace(LookIn As String, PatternStr As String, Optional ReplaceWith As String = "", _
    Optional ReplaceAll As Boolean = True, Optional MatchCase As Boolean = True) 
    
    ' This function uses Regular Expressions to parse a string, and replace parts of the string
    ' matching the specified pattern with another string.  The optional argument ReplaceAll controls
    ' whether all instances of the matched string are replaced (True) or just the first instance (False)
    
    ' By default, RegExp is case-sensitive in pattern-matching.  To keep this, omit MatchCase or
    ' set it to True
    
    ' If you use this function from Excel, you may substitute range references for all the arguments
    
    Static RegX As Object
    
    If RegX Is Nothing Then Set RegX = CreateObject("VBScript.RegExp")
    With RegX
        .Pattern = PatternStr
        .Global = ReplaceAll
        .IgnoreCase = Not MatchCase
    End With
    
    RegExpReplace = RegX.Replace(LookIn, ReplaceWith)
        
End Function

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question