Hi,
I am trying to create an update statement that will replace values
based on a wild card and only replace the wild card data (not the
entire field). Does anyone have any suggestions? Is this even
possible?
Here is my code:
UPDATE
UPDATE MyTable
SET MyField = replace(MyField, '*[0-9][0-9][0-9][0-9][0-9]*' ,
'XXXXX')
WHERE (MyTable.MyField) Like '*[0-9][0-9][0-9][0-9][0-9]*'
|
|
0
|
|
|
|
Reply
|
bcap
|
6/22/2010 1:58:28 PM |
|
bcap wrote:
> Hi,
>
> I am trying to create an update statement that will replace values
> based on a wild card and only replace the wild card data (not the
> entire field). Does anyone have any suggestions? Is this even
> possible?
>
> Here is my code:
>
> UPDATE
>
> UPDATE MyTable
> SET MyField = replace(MyField, '*[0-9][0-9][0-9][0-9][0-9]*' ,
> 'XXXXX')
> WHERE (MyTable.MyField) Like '*[0-9][0-9][0-9][0-9][0-9]*'
>
Replace() is a function. I suppose you could create a function in a
Module called WildcardReplace(OldValue, NewValue) and use Split() or
Instr() in the function to parse out and create the new string.
|
|
0
|
|
|
|
Reply
|
Salad
|
6/22/2010 2:17:25 PM
|
|
Hi,
Thank you very much for the response. So are you suggeting that I
remove all characters that are not numeric from my field using a SPLIT
Function? Do you happen to have an example of this?
Thank you again!
|
|
0
|
|
|
|
Reply
|
bcap
|
6/22/2010 3:06:56 PM
|
|
Replace does not work with wildcards, neither does Instr
You need to write a custom VBA function to do this or use something more
powerful in terms of string manipulation - i.e., REGEX (Regular Expressions)
For the specific case you might use something like the following to replace
one incidence of five numbers in a row. With a little work this could be
generalized. As written you would have to only pass in values that have a
string of 5 digits - otherwise you will get null returned.
Public Function fReplace5Numbers(strIN)
Dim i as Long
For i = 1 to Len(strIn)
IF Mid(strIn,1,5) Like "[0-9][0-9][0-9][0-9][0-9" Then
fReplace5Numbers = Left(strIn,i-1) & "xxxxx" & Mid(strIn,i+5)
exit for
End If
Next i
END Function
STEP 1: BACKUP your data before attempting the following.
STEP 2: BACKUP your data before attempting the following.
Without a backup you cannot restore the data if this does not work the way you
expect.
UPDATE MyTable
SET MyField = fReplace5Numbers(MyField)
WHERE (MyTable.MyField) Like '*[0-9][0-9][0-9][0-9][0-9]*'
bcap wrote:
> Hi,
>
> I am trying to create an update statement that will replace values
> based on a wild card and only replace the wild card data (not the
> entire field). Does anyone have any suggestions? Is this even
> possible?
>
> Here is my code:
>
> UPDATE
>
> UPDATE MyTable
> SET MyField = replace(MyField, '*[0-9][0-9][0-9][0-9][0-9]*' ,
> 'XXXXX')
> WHERE (MyTable.MyField) Like '*[0-9][0-9][0-9][0-9][0-9]*'
>
--
John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
|
|
0
|
|
|
|
Reply
|
John
|
6/22/2010 4:50:55 PM
|
|
bcap wrote:
> Hi,
>
> Thank you very much for the response. So are you suggeting that I
> remove all characters that are not numeric from my field using a SPLIT
> Function? Do you happen to have an example of this?
>
> Thank you again!
>
I thought you were replacing a string, not a range. Maybe this will
provide a direction.
Sub Junk()
Dim MyField As String
Dim MyNewField As String
MyField = "The current value is " & _
"123ABC [0-9][0-9][0-9][0-9][0-9] and I want " & _
"[0-9][0-9][0-9][0-9][0-9] to be XXXXX How Do I Do That"
MyNewField = ConvertJunk(MyField, "09", "XXXXX")
End Sub
Function ConvertJunk(strOld As String, strRange As String, _
strNewVal As String) As String
Dim intFor As Integer
Dim strNew As String
ConvertJunk = strOld
'I assumed number, you could use ASC for chars.
For intFor = Left(strRange, 1) To Right(strRange, 1)
ConvertJunk = Replace(ConvertJunk, intFor, strNewVal)
Next
MsgBox "it was " & strOld & vbNewLine & vbNewLine & _
" and now " & ConvertJunk
End Function
|
|
0
|
|
|
|
Reply
|
Salad
|
6/22/2010 5:09:01 PM
|
|
Thank you John and Salad for your help, very much
appreciated!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! =)
|
|
0
|
|
|
|
Reply
|
bcap
|
6/22/2010 8:33:25 PM
|
|
Hi John, what would I change to be able to search for 5 consecutive
numbers anywhere within the string? Thank you again!
|
|
0
|
|
|
|
Reply
|
bcap
|
6/23/2010 1:34:29 PM
|
|
bcap wrote:
> Hi John, what would I change to be able to search for 5 consecutive
> numbers anywhere within the string? Thank you again!
Sub String5()
Dim intFor As Integer
Dim strNum As String
Dim strToSearch As String
'display number and next 4
strNum = "1"
For intFor = CInt(strNum) To CInt(strNum) + 4
MsgBox intFor
Next
'display numbers in string
strNum = "13579"
For intFor = 1 To Len(strNum)
MsgBox Mid(strNum, intFor, 1)
Next
'display only 1 and 9
strToSearch = "19"
For intFor = 1 To Len(strNum)
If InStr(strToSearch, Mid(strNum, intFor, 1)) Then
MsgBox Mid(strNum, intFor, 1)
End If
Next
End Sub
|
|
0
|
|
|
|
Reply
|
Salad
|
6/23/2010 2:27:46 PM
|
|
Sorry, I don't understand the question.
Do you want to replace every instance of 5 consecutive numbers in a string?
That is if the string were
aaas 12345 zzzzz67890 jj
then you want
aaas zzzzz jj
returned?
You could call the current function repeatedly to do so.
I will take a look at this and try to get back to you tomorrow. By the way, I
noticed a typo in the posting. I dropped an ending bracket in the like clause
Public Function fReplace5Numbers(strIN)
Dim i as Long
For i = 1 to Len(strIn)
IF Mid(strIn,1,5) Like "[0-9][0-9][0-9][0-9][0-9]" Then
fReplace5Numbers = Left(strIn,i-1) & "xxxxx" & Mid(strIn,i+5)
exit for
End If
Next i
END Function
bcap wrote:
> Hi John, what would I change to be able to search for 5 consecutive
> numbers anywhere within the string? Thank you again!
--
John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
|
|
0
|
|
|
|
Reply
|
John
|
6/23/2010 7:56:17 PM
|
|
Hi John,
I am sorry for the confusion and sincerely appreciate your responses
(Salad=92s too)!!!!
Basically what is going on here is I have a Text field and we want to
remove Invoice Numbers from the table and replace the invoice numbers
with five X=92s in place of the original number. The tricky part is
that all the data was entered free hand and uncontrolled so there are
different methods to how they are entered.
The data entry has been fairly random. Some fields have just the
invoice number, others have it surround my comments.
For example:
ID Comment
1 Invoice #55555 has been paid.
2 53412
3 87292 has been paid on 6/23/2010
4 Invoice Num: 55 322
What I would like to be able to do is make the data look like this
using the examples above:
ID Comment
1 Invoice #XXXXX has been paid.
2 XXXXX
3 XXXXX has been paid on 6/23/2010
4 Invoice Num: XXXXX
I hope this helps clarify the problem and once again thank you!!!!
|
|
0
|
|
|
|
Reply
|
bcap
|
6/23/2010 9:03:21 PM
|
|
Ok, I tested this one and got out the bugs (I hope). It will replace only one
occurrence per field.
I am working on a more general procedure that will replace all or a specified
number of matches of any wild card string that is a defined length - you
cannot use * in the wild card string. I have made some progress, but I have
to figure out a way to determine the length of the wild card string.
Try this function and see if it will do what you want.
Public Function fReplace5Numbers(strIN)
Dim i As Long
If Len(strIN & "") = 0 Or Not strIN Like "[0-9][0-9][0-9][0-9][0-9]" Then
fReplace5Numbers = strIN
Else
For i = 1 To Len(strIN)
If Mid(strIN, i, 5) Like "[0-9][0-9][0-9][0-9][0-9]" Then
fReplace5Numbers = Left(strIN, i - 1) & "xxxxx" & Mid(strIN, i + 5)
Exit For
End If
Next i
End If
This function will not work on example number 4 since it has spaces in the
number. The function looks for an exact match of 5 number characters in a
row. So it will replace a 5-character postal code just as easily as it
replaces a 5-character invoice number. Or if the invoice is 6 numbers long it
will replace the first 5 characters.
bcap wrote:
> Hi John,
>
> I am sorry for the confusion and sincerely appreciate your responses
> (Salad�s too)!!!!
>
> Basically what is going on here is I have a Text field and we want to
> remove Invoice Numbers from the table and replace the invoice numbers
> with five X�s in place of the original number. The tricky part is
> that all the data was entered free hand and uncontrolled so there are
> different methods to how they are entered.
>
> The data entry has been fairly random. Some fields have just the
> invoice number, others have it surround my comments.
>
> For example:
>
> ID Comment
> 1 Invoice #55555 has been paid.
> 2 53412
> 3 87292 has been paid on 6/23/2010
> 4 Invoice Num: 55 322
>
> What I would like to be able to do is make the data look like this
> using the examples above:
>
> ID Comment
> 1 Invoice #XXXXX has been paid.
> 2 XXXXX
> 3 XXXXX has been paid on 6/23/2010
> 4 Invoice Num: XXXXX
>
> I hope this helps clarify the problem and once again thank you!!!!
--
John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
|
|
0
|
|
|
|
Reply
|
John
|
6/24/2010 1:08:17 PM
|
|
Thanks again John! It seems like this is only updating records to
xxxxx if the first set of characters are the consecutive numbers we
are looking for.
|
|
0
|
|
|
|
Reply
|
bcap
|
6/24/2010 1:37:56 PM
|
|
OH good gravy!!!! Try this corrected version.
I need either more coffee or less coffee. I forgot to add the any number of
characters wildcards in the initial test.
Public Function fReplace5Numbers(strIN)
Dim i As Long
If Len(strIN & "") = 0 Or Not strIN Like "*[0-9][0-9][0-9][0-9][0-9]*" Then
fReplace5Numbers = strIN
Else
For i = 1 To Len(strIN)
If Mid(strIN, i, 5) Like "[0-9][0-9][0-9][0-9][0-9]" Then
fReplace5Numbers = Left(strIN, i - 1) & "xxxxx" & Mid(strIN, i + 5)
Exit For
End If
Next i
End If
End Function
And now looking at it I could probably change the initial test to
IF Not strIn & "" LIKE "*[0-9][0-9][0-9][0-9][0-9]*" Then
fReplace5Numbers = strIN
ELSE
.....
bcap wrote:
> Thanks again John! It seems like this is only updating records to
> xxxxx if the first set of characters are the consecutive numbers we
> are looking for.
--
John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
|
|
0
|
|
|
|
Reply
|
John
|
6/24/2010 4:43:10 PM
|
|
THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
0
|
|
|
|
Reply
|
rayh (13)
|
6/24/2010 7:40:38 PM
|
|
|
13 Replies
843 Views
(page loaded in 0.136 seconds)
Similiar Articles: Update Statement - Replace, Wildcard, and partial update? - comp ...Hi, I am trying to create an update statement that will replace values based on a wild card and only replace the wild card data (not the entire f... Updating numeric values from shell script - comp.unix.shell ...Update Statement - Replace, Wildcard, and partial update? - comp ... Hi, I am trying to create an update statement that will replace values ... remove all characters that ... Bash Test for Partial Match of String - comp.unix.shellUpdate Statement - Replace, Wildcard, and partial update? - comp ... Bash Test for Partial Match of String - comp.unix.shell Update Statement - Replace, Wildcard, and ... Sharepoint linked table - can add records but can't update ...Update Statement - Replace, Wildcard, and partial update? - comp ... I forgot to add the any number of ... regex in KSH - comp.unix.shell I will update my link ... Wildcard match? - comp.unix.shellUpdate Statement - Replace, Wildcard, and partial update? - comp ... The function looks for an exact match of 5 number characters in a row. ... Update Statement - Replace ... Enform query - Count, sort with partial string - comp.sys.tandem ...Update Statement - Replace, Wildcard, and partial update? - comp ... Enform query - Count, sort with partial string - comp.sys.tandem ... Update Statement - Replace ... remove junk characters - comp.lang.perl.miscUpdate Statement - Replace, Wildcard, and partial update? - comp ... So are you suggeting that I remove all characters that are not numeric from my field using a ... search for a string in a file using expect.. - comp.lang.tcl ...Update Statement - Replace, Wildcard, and partial update? - comp ... search for a string in a file using expect.. - comp.lang.tcl ..... string length $user] set old_num ... Replacing ONLY the first occurence of a substring in a string ...Update Statement - Replace, Wildcard, and partial update? - comp ... > I thought you were replacing a string, not a range. ... It will replace only one occurrence per ... Heads Up on SQL92 Mode - comp.databases.ms-accessUpdate Statement - Replace, Wildcard, and partial update? - comp ... difference between striping using mdadm and LVM - comp.os.linux ..... must be stated explicitly in the ... Script to count occurrences - comp.unix.programmerUpdate Statement - Replace, Wildcard, and partial update? - comp ... If I change the name of a layout will the scripts update ? - comp ... Update Statement - Replace ... Dimming Display - comp.sys.mac.systemUpdate Statement - Replace, Wildcard, and partial update? - comp ... Sub String5() Dim intFor As Integer Dim strNum As String Dim strToSearch As String 'display number and ... Use of quotation marks and apostrophes in SQL and Criteria ...... need to escape the double quotes when assigning this statement to a variable: sSQL = "Update ... and run > without modification (unless you need to replace wildcards ... find string matching whole word - comp.soft-sys.matlab> > > > I am doing it using fprintf and the command regexprep but ... solution was to convert to strings, and then replace, but ... comp.lang.tcl ..... read everything into memeory, update ... [comp.publish.cdrom] CD-Recordable FAQ, Part 1/4 - comp.publish ...Archive-name: cdrom/cd-recordable/part1 Posting-Frequency: monthly Last-modified: 2008/10/09 Version: 2.71 Send corrections and updates to And... Update Statement - Replace, Wildcard, and partial update? DataBaseHi, I am trying to create an update statement that will replace values based on a wild card and only replace the wild card data (not the entire field). D Update Statement - Replace, Wildcard, and partial update? - comp ...Hi, I am trying to create an update statement that will replace values based on a wild card and only replace the wild card data (not the entire f... 7/23/2012 2:23:01 AM
|