Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-07-01
3
Medium Priority
?
565 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 93

Accepted Solution

by:
Patrick Matthews earned 200 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 93

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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: …

610 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