通过忽略其他字符进行列比较

时间:2013-06-27 23:23:45

标签: ms-access-2007

如何通过忽略其他字符来比较MS Access中的两列。',"

例如,下列两列数据应该相符

Col1         Col2

"adA.'kd,"  adA.kd

感谢您的回复

由于

2 个答案:

答案 0 :(得分:2)

如果您只想忽略几个字符,可以尝试使用空字符串replacing。更好的方法是进行正则表达式替换,例如this,其中正则表达式模式(在您的情况下patrn变量将是正则表达式“[。,'\”]“忽略句点,逗号,单引号和双引号(但我不确定转义如何在MS Access中工作),而replStr只是“”,空字符串)

答案 1 :(得分:2)

如果您打算使用Regex,那么您可以在替换中使用字符类\W(相当于[^\w]),这意味着任何非单词字符。这不包括数字和下划线。如果您想要替换这些,请考虑[^A-Za-z];也就是说,任何不是字母的东西。

访问函数包括Replace,但它对此不是很有用,因为它不允许我们指定要替换的一组字符。如果使用Access Query执行比较,则可以创建可在查询中使用的VBA函数,类似于以下内容。

Function GetAlpha(strInput As String) As String
    'Returns a string containing only the letters from the input
    Dim strCheck As String
    Dim i As Integer
    Dim iLen As Integer
    Dim strOutput As String
    Dim ch As String

    strCheck = strInput   'remember the original input
    strInput = LCase(strInput)    'ignore case temporarily
    iLen = Len(strInput)
    For i = 1 To iLen
        ch = Mid(strInput, i, 1)
        If ch >= "a" And ch <= "z" Then
            'if a letter, build a string using the original letter from the input
            strOutput = strOutput & Mid(strCheck, i, 1)
        End If
    Next i
    GetAlpha = strOutput
End Function

在Access查询中,您将创建一个表达式,例如

IIF(GetAlpha([Field1])=GetAlpha([Field2]),"Same","Different")

访问忽略了案例。如果您不想包含区分大小写,那么您也可以使用StrComp(),或者修改我的函数以包含两个(字符串)参数并返回一个布尔值。您可以使用以下内容而不是修改功能:

Function CompareAlpha(strInput1 As String, strInput2 As String) As Boolean
    'case-insensitive comparison of the letters of two strings
    CompareAlpha = (StrComp(GetAlpha(strInput1), GetAlpha(strInput2), _
        vbBinaryCompare) = 0)
End Function

以下函数与上面的GetAlpha()完全相同,但可能稍微整洁:

Function GetAlpha(strInput As String) As String
    Dim i As Integer
    Dim iLen As Integer
    Dim strOutput As String
    Dim ch As String

    iLen = Len(strInput)
    For i = 1 To iLen
        ch = LCase(Mid(strInput, i, 1))
        If ch >= "a" And ch <= "z" Then
            strOutput = strOutput & Mid(strInput, i, 1)
        End If
    Next i
    GetAlpha = strOutput
End Function

已编辑:以下类似函数检查字符串是否仅包含字母。 (我把它包括在内,以防其他人使用。)

Function CheckAlpha(strInput As String) As Boolean
    Dim strCheck As String
    Dim i As Integer
    Dim iLen As Integer
    Dim strOutput As String
    Dim ch As String

    strCheck = strInput    'store the original input to compare later
    strInput = LCase(strInput)    'temporarily ignore case
    iLen = Len(strInput)
    For i = 1 To iLen
        ch = Mid(strInput, i, 1)
        If ch >= "a" And ch <= "z" Then
            'construct a string of only the letters from the original input
            strOutput = strOutput & Mid(strCheck, i, 1)
        End If
    Next i
    'is the letters-only version the same as the original input?
    CheckAlpha = (strCheck = strOutput)
    'use StrComp() for case-sensitivity. That is, we could use StrComp() to 
    'check if all letters were upper, or lower, case.
End Function

已添加在回复评论时,如果数据可能包含NULL值,则以下修订版将返回空字符串“”而不是Type Mismatch错误。另一种方法是在调用GetAlpha()之前排除NULL。

Function GetAlpha(varInput As Variant) As String
    Dim i As Integer
    Dim iLen As Integer
    Dim strOutput As String
    Dim ch As String

    If IsNull(varInput) Then
        GetAlpha = ""
        Exit Function
    End If
    iLen = Len(varInput)
    For i = 1 To iLen
        ch = LCase(Mid(varInput, i, 1))
        If ch >= "a" And ch <= "z" Then
            strOutput = strOutput & Mid(varInput, i, 1)
        End If
    Next i
    GetAlpha = strOutput
End Function