Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Greetings !!
Today someone asked me to solve a query to find all account name which does not contains 'A' anywhere in the column 'acname' using Like opertor and not to use 'Not Like'. So, I thought we can use '^' or '!' to solve the query but it did not work.

I tried this query
select * from acmast where acname like '%[!a]%'

but fail to resolve this query using '!'

So, I need to know what am I missing


Best Regards,
Sukhen Dass

What I have tried:

select * from acmast where acname like '%[!a]%'
Posted
Updated 15-Aug-18 1:50am

It's really easy to figure out if you read the documentation on LIKE:

LIKE (Transact-SQL) | Microsoft Docs[^]
 
Share this answer
 
Comments
sukhen dass 14-Aug-18 1:14am    
Have read this docs many time but did not find solution as nowhere is given uses of '%[!anyCharacter]%'. only given is either from first not like('[!anyCharacter]%') or from last not like('%[!anyCharacter]') but not for full search
Dave Kreskowiak 14-Aug-18 9:27am    
What you missed was reading the documentation and performing tests to further your understanding. Experiment with WHERE clauses. Figure out exactly how these things work and what their limits are.

"What you tried" says you tried only a single query. It also says you only tried with a "!" character in the query. Reading the documentation, that character isn't even mentioned as a possibility that does anything useful.

"!" isn't even used as a standalone operator of any kind. Google for "TSQL operators" to find out.

This is the world of doing thorough research and it's a HUGE part of development work.

Oh, and to directly answer your question, no it's not possible to use ^ in a LIKE expression to "not find a character" anywhere in a string.
The point Dave is making is that you do not use ! or ^ to indicate "not" in SQL, so that the thing you are "missing" is understanding the syntax of the tool you are trying to use.

There are various techniques that you can use to avoid using NOT LIKE (or NOT IN) here is one example:
SQL
create table demo ([data] varchar(255))
insert into demo ([data]) values
('words without the offending letter'),
('sentence that contains the letter a')

select *
from demo
where patindex('%a%', [data]) = 0
This returns
words without the offending letter

Documentation PATINDEX (Transact-SQL) | Microsoft Docs[^]

Alternatives can be found via Google[^]
 
Share this answer
 
Comments
sukhen dass 14-Aug-18 5:52am    
Point is not that we have alternative the question is this possible using ! or ^ or not
Dave Kreskowiak 14-Aug-18 8:52am    
That's not what your question says. It says YOU don't understand how those characters can be used. That's what the documentation is for, and, yes, it's in the doc I linked to.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900