Microsoft Access中的扫描功能?

时间:2013-01-10 16:18:18

标签: ms-access

我尝试根据文本字段中的空格字符进行子字符串,但我无法成功完成。

我的领域有两个但有时三个单词都是空格分开的。我希望能够通过匹配空间来拉出第二个或第三个单词。这个词的长度可变。

我尝试过这样的事情,但没有成功。

select mid([genus],InStr([genus]," ")+1) AS species from <tablename>;

有什么建议吗? 谢谢。

2 个答案:

答案 0 :(得分:2)

最后一句话你可以:

SELECT Mid(genus,1 + InStrRev(genus," ")) AS species

对于第二个单词,如果有2个或更多;

SELECT mid(genus,InStr(genus," ")+1, instr(InStr(genus," "),genus, " ")-1)  AS species

答案 1 :(得分:1)

您可以使用此功能

Public Function Item(ByVal s As String, ByVal index As Long, _
                     Optional ByVal delimiter As String = " ") As String
    Dim i As Long, pos1 As Long, pos2 As Long

    If index < 1 Or index > Len(s) Then
        Item = ""
        Exit Function
    End If
    s = s & delimiter
    pos2 = 1 - Len(delimiter)
    For i = 1 To index
        pos1 = pos2 + Len(delimiter)
        pos2 = InStr(pos1, s, delimiter, vbBinaryCompare)
        If pos2 = 0 Then
            Item = ""
            Exit Function
        End If
    Next i
    Item = Mid$(s, pos1, pos2 - pos1)
End Function

测试

Item("asdas df 4354 sdf", 3)

产量

"4354"

在你的选择陈述中

SELECT
    Item(genus, 1) AS word1,
    Item(genus, 2) AS word2,
    Item(genus, 3) AS word3
FROM
    <tablename>;