0

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

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

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

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

有什么建议么?谢谢你。

4

2 回答 2

2

对于最后一句话,您可以:

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

如果有 2 个或更多,则为第 2 个单词;

SELECT mid(genus,InStr(genus," ")+1, instr(InStr(genus," "),genus, " ")-1)  AS species
于 2013-01-10T16:26:08.023 回答
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>;
于 2013-01-10T16:42:29.923 回答