Trying to join Access tables with like statement with list in field

独自空忆成欢 提交于 2019-12-11 08:36:23

问题


I have a problem that I have been hunting for a solution to, but to avail. The basics are that I am trying to join 2 tables in Access by comparing a value in a field of Table 1 to a field in Table 2 that contains the number concatenated along with a few others in a list type format. (both fields are text type)

Example.
Table1.CWT value = 640242
Corresponding Table2.TAG_NO value I want to match to = 640242; 635894; 058426

So that it links the two tables based on the common value (640242 in this case).
So far, I have tried the following:

LEFT JOIN [Table2] ON [Table1].CWT like '*' & [Table2].TAG_NO & '*'

and

LEFT JOIN [Table2] ON [Table1].CWT & '*' like [Table2].TAG_NO

and what seems like every variation in between, I have even tried using % instead of *. But nothing works. In some cases, the value will be the second or third element in the string (635894 in above example), so I am looking for an option that will work in all cases. This is akin to looking for the equivalent of the CONTAINS function, but that does not seem to exist either.
Can anyone help me out?
Thanks
Ted


回答1:


You can use the Instr Function that tests if a string exists in other string as below:

Select [Table1].CWT, [Table1].OtherColumn, [Table2].Column1Needed,[Table2].Column2Needed  
From [Table1], [Table2]
Where Instr([Table2].TAG_NO,[Table1].CWT)>0

See http://www.techonthenet.com/access/functions/string/instr.php




回答2:


You need to switch the operands. And make sure that '640242' doesn't match '6402423', so add delimiters to both strings:

' ' & Table2.TAG_NO & ';' like '* ' & Table1.CWT & ';*'


来源:https://stackoverflow.com/questions/30267761/trying-to-join-access-tables-with-like-statement-with-list-in-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!