Access 2010 SQL Query find partial match in string of full word only

六眼飞鱼酱① 提交于 2019-12-03 16:48:21

Wildcards in SQL and access are a % symbol;

so

"SELECT tblEmployee.FullName, tblNames.Name FROM tblEmployee, tblNames
WHERE tblEmployee.FullName Like '%" & tblNames.Name & "%'"

Will match any instance of the string tblNames.Name

You can use the % at the start or end only to get the match of just the start of the string / end of the string repectively.

edit

Apologies, you can use * and ? in access as you expect, however are you sending the query as a string to some VBA code? Or trying to run it directly? As directly in a query it'll not be able to parse the tblNames.Name string into the query and you'll need to pass that in from a form or other peice of code.

Edit based on comment

To select a specific word it's just a bit of a work around required:

SELECT * FROM table WHERE field LIKE '* myWord *' OR field LIKE '* myWord.*'

You can optionally caputure start of sentances `LIKE 'MyWord *' and word with commas, fullstops, exclamtion marks etc...

You could do an IN statement and have all the variations in a lookup table to keep it easy to maintain as another option.

I used this solution from the edited post in Access 2007 and it worked perfectly!

WHERE " " & tblEmployee.FullName & " " Like "* " & tblNames.Name & " *"

Now my question is, what the heck is the SQL code doing and why does it work? Can someone break it down?

EDITED:

OK, my confusion arose from the different use of characters in Access SQL vs more standard SQL. & is concatenate in Access, while + is concatnate in standard SQL. And * is the wildcard for any number of characters in Access while it is % in SQL.

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