[英]In MS Access, How do I extract multiple values in a single column?
如何从单个列中解析多个值? 问题是多个值被无关的(出于我的目的)数据包围。 例子:
Select * from my_table:
Fname Lname Data
Fred Smith @3aXXXX;Name:AA;@43deXXXX;Name:BB;@5433ed9;NAME:ABC;*#!XXXXXXXX;NAME:MyPetDog;@#IDXXXX
对于数据列,我想从列中提取“名称:”字段后面的所有值。 那将是“名称:”之后和“;”之前的文本 . 在上面的例子中:
Select Fname, Lname, [DATA] from my_table
Fname Lname [*Parsed* DATA]
Fred Smith AA,BB, ABC, MyPetDog
解决上述问题将是一个巨大的帮助。 但是,我真正想做的是查找/替换(SWITCH 等)从字符串返回的每个值:
Fname Lname [Translated DATA]
Fred Smith Airport, Bus Station, Restaurant, FIDO
对于使用名义数据,我深表歉意。 我的实际脚本(在另一个网络上)涉及几个表连接以获取列 DATA。 我只是不知道如何从这个大字符串中提取特定值(其他提取完整数据集并使用 AWk 或 MS Excel 之后清理数据)。
感谢有关解决此问题的任何帮助或提示。
凯文·L。
我建议创建一个小的 VBA function,它获取“丑陋”数据并使用Split
function 将其拆分。 就像是:
Public Function fSplitData(strData As String) As String
Dim aData() As String
Dim lngLoop1 As Long
aData = Split(strData, ";")
For lngLoop1 = LBound(aData) To UBound(aData)
If Left(aData(lngLoop1), 5) = "Name:" Then
fSplitData = fSplitData & Mid(aData(lngLoop1), 6) & ","
End If
Next lngLoop1
If Right(fSplitData, 1) = "," Then fSplitData = Left(fSplitData, Len(fSplitData) - 1)
End Function
这给出了所需的 output :
AA,BB,ABC,MyPetDog
而且,您可以使用记录集从查找表中获取数据,而不仅仅是连接提取的值:
Public Function fSplitData(strData As String) As String
Dim db As DAO.Database
Dim rsLookup As DAO.Recordset
Dim strLookup As String
Dim aData() As String
Dim lngLoop1 As Long
aData = Split(strData, ";")
Set db = CurrentDb
For lngLoop1 = LBound(aData) To UBound(aData)
If Left(aData(lngLoop1), 5) = "Name:" Then
strLookup = Mid(aData(lngLoop1), 6) & ","
Set rsLookup = db.OpenRecordset("SELECT LookupData FROM tblName WHERE RawData='" & strLookup & "';")
If Not (rsLookup.BOF And rsLookup.EOF) Then
fSplitData = fSplitData & rsLookup!LookupData & ","
End If
End If
Next lngLoop1
If Right(fSplitData, 1) = "," Then fSplitData = Left(fSplitData, Len(fSplitData) - 1)
End Function
问候,
问候,
感谢@Applecore,我有一个几乎可以工作的解决方案(虽然可能不是最好的解决方案):
SELECT Fname,
Lname,
SWITCH(data like "*AA*","Airport")&", "&
SWITCH(data like "*BB*", "Bus Station")&", "&
SWITCH(data like "*ABC*", "Restaurant")&", "&
SWITCH(data like "*MyPetDog*","FIDO") as DATA
Fname Lname [Translated DATA]
Fred Smith Airport, Bus Station, Restaurant, FIDO
唯一的问题是,如果一个值不存在,那么我会得到额外的逗号。 例如,如果 BB 是唯一存在的值,那么我得到:
Fname Lname [Translated DATA]
Fred Smith , Bus Station, ,
正如@AppleCore 建议的那样,使用外部 function 来避免混乱。
SELECT
Fname,
Lname,
CleanField([Data]) AS TranslatedData
FROM
my_table;
将 function(使用Select Case..
用于转换)复制粘贴到新模块 go 到菜单Debug、Compile中,然后将 Module1 模块保存为
Public Function CleanField(ByVal Value As String) As String
Dim Parts As Variant
Dim Index As Integer
Dim Part As String
Dim Text As String
Parts = Split(";" & Value, ":")
For Index = LBound(Parts) To UBound(Parts)
Part = Split(Parts(Index), ";")(0)
Select Case Part
Case Is = "AA"
Part = "Airport"
Case Is = "BB"
Part = "Bus station"
Case Is = "ABC"
Part = "Restaurant"
Case Is = "MyPetDog"
Part = "FIDO"
End Select
If Part <> "" Then
If Text <> "" Then
Text = Text & ", "
End If
Text = Text & Part
End If
Next
CleanField = Text
End Function
例子:
Fname Lname TranslatedData
------- ------- --------------------------------------
Fred Smith Airport, Bus station, Restaurant, FIDO
George Olsen Airport, Restaurant, Bus station, FIDO
Tina Doe Restaurant, Bus station, FIDO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.