We help IT Professionals succeed at work.

Many NOT LIKE conditions in a Totals Query

Last Modified: 2018-08-24

In the following Sql, if I have two NOT LIKE conditions in the HAVING clause.
SELECT [CONCUR 2018_01-04].TripNumber, [CONCUR 2018_01-04].BusinessPurpose
FROM [CONCUR 2018_01-04]
GROUP BY [CONCUR 2018_01-04].TripNumber, [CONCUR 2018_01-04].BusinessPurpose
HAVING ((([CONCUR 2018_01-04].BusinessPurpose) Not Like "*Party*" And ([CONCUR 2018_01-04].BusinessPurpose) Not Like "*Flowers*"));

Open in new window

1.  If I have 40 or 50 NOT LIKE conditions, is there a better way to write the query than writing so many expressions in the HAVING clause
2. Is there a way to manage the NOT LIKE keywords in another table (that users can add to the list on a form) and have the keywords feed into the NOT LIKE conditions in the query?

Watch Question

Kyle AbrahamsSenior .Net Developer


You can do a Concat on a table: CONCAT('*',REPLACE(field,'*','\*'),'*')

in order to do this you would create a new table, say ValidBusinessPurposes
This could have 2 columns, tripnumber ValidBusinessPurpose

I then flipped your logic to look for a match for a valid business purpose, and if a match is found, then exclude it from the result.
SELECT [CONCUR 2018_01-04].TripNumber, [CONCUR 2018_01-04].BusinessPurpose
FROM [CONCUR 2018_01-04] 
left join [ValidBusinessPurpose] as vbp on  [CONCUR 2018_01-04].TripNumber = vbp.TripNumber and  [CONCUR 2018_01-04].BusinessPurpose  like CONCAT('*',REPLACE(vbp.ValidBusinessPurpose,'*','\*'),'*') 
-- you're looking for something without a valid business purpose.
vbp.ValidBusinessPurpose is null 
GROUP BY [CONCUR 2018_01-04].TripNumber, [CONCUR 2018_01-04].BusinessPurpose

Open in new window

Most Valuable Expert 2015
Distinguished Expert 2018
This one is on us!
(Get your first solution completely free - no credit card required)
DavisroBudget Analyst


Gustav, yes you did. but I didn't understand something...in your example
Select *
From YourTable
Where RTrim(Left([Description], Instr([Description], " "))) Not In (Select KeyWord From tblKeywords)
I didn't understand how to get the keywords from my table into your Not In clause. Is this a subquery?

What did work for me is to create an Excluded Records table
SELECT DISTINCT tblConcurTrips.TripNumber
FROM tblExclusionWords, tblConcurTrips
WHERE (((InStr([tblConcurTrips].[BusinessPurpose],[tblExclusionWords].[Keywords]))>0));

And then do a Not Match query to remove the Excluded Records and it works
SELECT CONCUR.TripNumber, CONCUR.BusinessPurpose
FROM CONCUR LEFT JOIN qTripsExclude ON CONCUR.TripNumber = qTripsExclude.TripNumber
GROUP BY CONCUR.TripNumber, CONCUR.BusinessPurpose, qTripsExclude.TripNumber
HAVING (((qTripsExclude.TripNumber) Is Null));
Most Valuable Expert 2015
Distinguished Expert 2018

This was the keywords:

Select KeyWord From tblKeywords

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.