【问题标题】:SQL: Select Character between Multiple Known StringsSQL:在多个已知字符串之间选择字符
【发布时间】:2015-04-17 22:11:43
【问题描述】:

我只想选择在括号“()”之间找到的字符我找到了可以在同一字符串之间选择字符的代码:

Public Function GetStuffYouWant(ByVal pInput As Variant, _
        Optional pSplitChar As String = "-") As Variant
    Dim varResult As Variant
    Dim varPieces As Variant

    If IsNull(pInput) Then
        varResult = Null
    Else
        varPieces = Split(pInput, pSplitChar)
        If UBound(varPieces) > 1 Then
            varResult = varPieces(1)
        Else
            varResult = Null
        End If
    End If
    GetStuffYouWant = varResult
End Function

效果很好,因为当有空值时,我不会收到空错误。

问题是我需要在两个已知字符串之间选择字符。我发现这段代码查看了两个字符串,但我不知道如何将其写入第一个代码以获得我想要的结果:

   dim first as integer
  dim second as integer
   dim result as string
   first = instr(1,"yourtext","-")
    second = instr(first+1,"yourtext","-")

   if first > 0 and second > first then
           second = second - first
            result = mid("yourtext",first+1, second-1)
     end if

这是我需要的一个例子:

Before:                         I need: 
    Issue                       Issue
    ------                      ------
1   (Dog) at the carpet         Dog at the carpet
2                                                 <---Not a null error
3   (Cat) dog                   Cat

【问题讨论】:

  • 您要的是 SQL 还是 VB.net?
  • 好问题。我会接受任何将 () 与文本分开而不给我空错误的解决方案。

标签: sql vba ms-access ms-access-2013 jet


【解决方案1】:

此代码将解析您的“varPieces(1)”以获取“(”和“)”内的文本

Public Function GetStuffYouWant(ByVal pInput As Variant, _
        Optional pSplitChar As String = "-") As Variant
    Dim varResult As Variant
    Dim varPieces As Variant

    If IsNull(pInput) Then
        varResult = Null
    Else
        varPieces = Split(pInput, pSplitChar)
        If UBound(varPieces) > 1 Then
            varResult = GetStuffYouWant_Parse(varPieces(1))
        Else
            varResult = Null
        End If
    End If
    GetStuffYouWant = varResult
End Function

Public Function GetStuffYouWant_Parse(ByVal pInput As String) As String
   dim first as integer
   dim second as integer
   dim result as string

   first = instr(1,pInput ,"(")
   second = instr(first+1,pInput,")")

   if first > 0 and second > first then
           second = second - first
           result = mid(pInput,first+1, second-1)
   end if

   GetStuffYouWant_Parse = result
End Function

【讨论】:

  • 这几乎可以工作,但是当值为 null 时我得到“错误”
猜你喜欢
  • 1970-01-01
  • 2013-08-24
  • 2018-06-24
  • 1970-01-01
  • 2020-11-13
  • 2020-11-05
  • 2013-07-25
  • 1970-01-01
  • 2011-01-30
相关资源
最近更新 更多