Solved

MS Access SQL Query to get records where a certain string is within a field

Posted on 2013-06-12
6
436 Views
Last Modified: 2013-06-12
Hi,
I am trying to construct an MS Access 2010 query that returns me some rows where the word "knotweed" occurs in concatenation of a number of Note Fields, this was my first idea:

SELECT Invoices.[Invoice Number], Invoices.fkCustomerID, Invoices.InvTotal,
Invoices.[Note 1] + Invoices.[Note 2] + Invoices.[Note 3] + Invoices.[Note 4] AS StrNotes
FROM Invoices
WHERE InStr(1,[StrNotes],"knotweed",1)>0;

This doesn't work as Access keeps throwing up an input box asking for the parameter StrNotes. So then I tried this:

SELECT Invoices.[Invoice Number], Invoices.fkCustomerID, Invoices.InvTotal,
Invoices.[Note 1] + Invoices.[Note 2] + Invoices.[Note 3] + Invoices.[Note 4] AS StrNotes, InStr(1,[StrNotes],"knotweed",1) AS FoundKnotWeed
FROM Invoices
WHERE [FoundKnotWeed] >0;

And still it thinks I am asking for a parameter called "FoundKnotWeed".

Can anyone tell me where I am going wrong??

Siv
0
Comment
Question by:Siv
  • 4
  • 2
6 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
Comment Utility
SELECT Invoices.[Invoice Number], Invoices.fkCustomerID, Invoices.InvTotal,
Invoices.[Note 1] + Invoices.[Note 2] + Invoices.[Note 3] + Invoices.[Note 4] AS StrNotes
FROM Invoices
WHERE InStr(1,Invoices.[Note 1] + Invoices.[Note 2] + Invoices.[Note 3] + Invoices.[Note 4],"knotweed",1)>0;
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Or:

SELECT Invoices.[Invoice Number], Invoices.fkCustomerID, Invoices.InvTotal,
Invoices.[Note 1] + Invoices.[Note 2] + Invoices.[Note 3] + Invoices.[Note 4] AS StrNotes
FROM Invoices
WHERE Invoices.[Note 1] Like "*knotweed*" Or
    Invoices.[Note 2] Like "*knotweed*" Or
    Invoices.[Note 3] Like "*knotweed*" Or
    Invoices.[Note 4] Like "*knotweed*"
0
 

Author Comment

by:Siv
Comment Utility
You little beauty!

OK, so why can't I concoct the field "strNotes" and then put that in the where clause???
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
It has to do with the order in which Jet is processing the commands.  The WHERE clause criteria are evaluated before expressions from the SELECT clause are available.
0
 

Author Closing Comment

by:Siv
Comment Utility
Patrick,

Thanks, that helps me understand going forward.
I just couldn't understand why it kept thinking I was putting in a parameter.
Thanks for your help.

Siv
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Glad to help :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now