以下是为拆分以下表而编写的查询。第三列是后缀,此字段前面的列不会在返回字段的末尾修剪-suffix。我附上了查询以及结果和问题的图像。
SELECT Left([ADDRESS_ID],InStr([ADDRESS_ID],"-")-1) AS Abbreviation,
Mid([ADDRESS_ID],InStr([ADDRESS_ID],"-")+1) AS CODE,
Right([ADDRESS_ID],InStr([ADDRESS_ID],"-")) AS SUFFIX,
MDM_Address_Remote_Key.ADDRESS_ID
FROM MDM_Address_Remote_Key;
三列分开,应该看起来像第三列;但是,中间列在末尾保持后缀。如何解决这个问题?
答案 0 :(得分:1)
原因是mid
函数需要3个参数:
Mid ( text, start_position, number_of_characters )
如果您没有指定最后一个参数,mid
函数将返回start_position之后的所有字符,这将导致您的结果。
尝试:
Mid([ADDRESS_ID],InStr([ADDRESS_ID],"-")+1,InStrRev([ADDRESS_ID],"-")-(InStr([ADDRESS_ID],"-")+1)) AS CODE
答案 1 :(得分:1)
此表达式提取我认为您想要CODE
的子字符串:
Mid([ADDRESS_ID], InStr(1, [ADDRESS_ID], "-") + 1, InStrRev([ADDRESS_ID], "-") - InStr(1, [ADDRESS_ID], "-") - 1) AS CODE
然而,这个表达式相当复杂,而你的其他字段表达式已经相当复杂了 - 整个lotta函数都在继续。
如果要在Access会话中运行查询,则可以使用利用VBA Split()
功能的自定义函数。
然后您的查询可以更简单明了。
SELECT SplitSegment([ADDRESS_ID], "-", 0) AS Abbreviation,
SplitSegment([ADDRESS_ID], "-", 1) AS CODE,
SplitSegment([ADDRESS_ID], "-", 2) AS SUFFIX,
MDM_Address_Remote_Key.ADDRESS_ID
FROM MDM_Address_Remote_Key;
Public Function SplitSegment(ByVal pSource As Variant, _
ByVal pDelimter As String, _
ByVal pIndex As Long) As Variant
Dim varOut As Variant
varOut = Null
If Not IsNull(pSource) Then
varOut = Split(pSource, pDelimter)(pIndex)
End If
SplitSegment = varOut
End Function