簡體   English   中英

訪問SQL>從某些文本中選擇“高度”

[英]Access SQL > Select “Height” out of some text

我有一個包含一些產品信息的Access數據庫。 不幸的是,高度,寬度等數據不在單獨的列中。 所以我想知道如何使用SQL來過濾/拆分這些值。

例如,它看起來像這樣:

Table: SHP_PRODUCT

Field: SHORT_DSC

Value: Candle "Country", Height 120mm, Diameter 50mm, red

Result should be: "120mm"

注意:高度並不總是與“它是第二個單詞”一樣在同一位置。 我也不能保證它是逗號分隔的。

您可以創建一個小函數來檢索此函數:

Public Function ExtractHeight(ByVal Value As String) As String

    Dim Parts As Variant

    Parts = Split(Value, "Height")
    If UBound(Parts) > LBound(Parts) Then
        ExtractHeight = Replace(Trim(Split(Parts)(1), " ")(0)), "m,", "m")
    End If

End Function

然后在查詢中使用它:

Height: ExtractHeight([SHORT_DSC])

如何使用RegEx?

Public Function extractHeight(ByVal val as String) As String
    Dim regEx As New VBScript_RegExp_55.RegExp
    Dim regExMatches As Object

    regEx.Pattern = "Height\s[\d*\,*\.*]+[a-z]{0,1}m"
    regEx.Global = False
    Set regExMatches = regEx.Execute(val)

    If regExMatches.Count > 0 Then
        extractHeight = Replace(regExMatches(0), "Height ", "")
    Else
        extractHeight = ""
    End If
End Function

如上所述在查詢中使用

正則表達式模式Height\\s[\\d*\\,*\\.*]+[az]{0,1}m匹配以“ Height”開頭,后跟一個空格,然后是任意長度的數字的字符串'。' 或',',然后是cm,mm,m之類的字符串。

確保將Microsoft VBScript Regular Expressions 5.5添加到VBA edtior中的引用( Extras - References

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM