相当简单。您可以编写一个 VBA 函数,将其嵌入到 SQL 查询中来执行此操作。我去了数据库工具(MS Access 顶部),Visual Basic,右键单击模块文件夹,插入 -> 模块。编写/复制代码后,转到 Visual Basic for Applications IDE 顶部的 Debug 并单击“Compile YourDatabaseName。
见下文:
Option Compare Database
Option Explicit
Public Function TrimString(fieldToTest As Variant) As Variant
Dim test, test2 As Variant
Dim trm As Variant
Dim intSearch, lngth As Integer
TrimString = Null
test = InStr(fieldToTest, "=")
test2 = InStr(fieldToTest, ":")
lngth = Len(fieldToTest)
If test > 0 Then
'We know it contains an equals sign
trm = Left(fieldToTest, test - 1)
trm = Trim(trm)
Debug.Print trm
ElseIf test2 > 0 Then
'We know it contains a colon
trm = Left(fieldToTest, test2 - 1)
trm = Trim(trm)
Debug.Print trm
ElseIf lngth > 0 Then
'Find out if it has integers in it
'Rebuild the string without Integers
For intSearch = 1 To lngth
If Not IsNumeric(Mid$(fieldToTest, intSearch, 1)) Then
trm = trm & Mid$(fieldToTest, intSearch, 1)
Else
End If
Next
trm = Trim(trm)
Else
'Regular String
'Do Nothing
trm = fieldToTest
End If
TrimString = trm
End Function
没有太多的错误处理,但我认为这回答了你的问题。
我把它扔到了一个表格里,字段数据类型是文本:
表格
ID stringTest (Field Name)
1. randomtext = 123453
2. otherrandmtext = 543555
3. evendifferentone : 453553
4. andsoon : 05453534
输出:
ID Expr1
1. randomtext
2. otherrandmtext
3. evendifferentone
4. andsoon
SQL:
SELECT Table2.ID,
TrimString([stringTest]) AS Expr1
FROM Table2;
从 VBA 代码中回忆,TrimString 是函数名。
如果有什么我忽略了 - 请告诉我,我会尽力纠正它。