MS访问查询从字符串中提取9位数字

时间:2018-09-12 01:35:54

标签: ms-access

我一直在Google上搜索,但是无法完全找到我需要的东西,使用这些功能是我的弱点。 基本上,我需要从表中的字母数字字符串中提取所有9位数字。它们将始终以0开头。数字不会有任何间断。 我还希望使用查询来检索9位数字。也就是说,将提取的号码单独放在单独的列中。只有1个9位数字

我有下面的代码,它拉出了数字,我只是不知道如何限制它的长度,因为字符串还有其他内容,例如地址的一部分和其他内容。

PHONE: Mid([DESCRIPTION],InStrRev([DESCRIPTION]," ",InStr([DESCRIPTION],"0"))+1,InStr(InStr([DESCRIPTION],"0"),[DESCRIPTION]," ")-InStrRev([DESCRIPTION]," ",InStr([DESCRIPTION],"0")))

2 个答案:

答案 0 :(得分:0)

您可以为此使用 Split

Public Function StripNine(ByVal Text As String) As String

    Dim Words   As Variant
    Dim Index   As Integer
    Dim Result  As String

    Words = Split(Text, " ")
    For Index = LBound(Words) To UBound(Words)
        If Len(Words(Index)) = 9 Then
            If IsNumeric(Words(Index)) Then
                Result = Words(Index)
                Exit For
            End If
        End If
    Next

    StripNine = Result

End Function

这将运行并返回如下示例:

s = "This is 1 message 00 with a 09-digit number 087654321 somewhere in the body"
? StripNine(s)
087654321

答案 1 :(得分:-1)

您可以使用Mid()功能。像这样:

Mid("Test1String", 1, 4) 'Result = "Test"
Mid("Test2String", 5, 3) 'Result = "2St"
Mid("Test3String", 7, 2) 'Result = "tr"

如果要消除空白,可以使用:

Trim(" Test4 String ") 'Result = "Test4 String"
Replace(" Test5 String ", " ", "") 'Result = "Test5String"

更新:

尝试一下:

Dim intPostion As Integer
Dim strResult As String

intPosition = InStr(YourString, 0) 'Search for the 0
strResult = Mid(YourString, intPosition, 9) 'Extract 9 digits

如果将其放在循环中(以便 intPosition 检查整个字符串),并检查 strResult 是否只有数字,则应该得到结果。