I have a table called logs.lognotes, and I want to find a faster way to search for customers who do not have a specific word or phrase in the note. I know I can use "not like", but my question is, can you use DOES NOT CONTAINS to replace not like, in the same way you can use:
SELECT *
FROM table
WHERE CONTAINS (column, ‘searchword’)
Yes, you should be able to use NOT on any boolean expression, as mentioned in the SQL Server Docs here. And, it would look something like this:
SELECT *
FROM table
WHERE NOT CONTAINS (column, ‘searchword’)
To search for records that do not contain the 'searchword' in the column. And, according to
Performance of like '%Query%' vs full text search CONTAINS query
this method should be faster than using LIKE with wildcards.
You can also simply use this:
select * from tablename where not(columnname like '%value%')
Related
I'm trying to create a request to find all the occurence of a name in my Usernames table:
for example I have:
SAnderso
BBobby
SAnderso1
SAnderso2
SAnderso99
and I'd like to get all the SAnderso(here 1,3,4,5)
here's what I tried:
SELECT * FROM Utilisateur WHERE NomUtilisateur LIKE 'SAnderso%[0123456789]' OR NomUtilisateur = 'SAnderso'
but when I do this nothing is shown in the results
can you help me ?
In any database, this might do want you want:
WHERE NomUtilisateur LIKE 'SAnderso%'
If you specifically need to check for numbers after the name, then the method depends on the database. In most databases, you'll need to use the extension for regular expressions.
In previous questions you have asked about MySQL so, assuming MySQL, you can use REGEXP_LIKE which allows regular expressions to be used:
SELECT * FROM Utilisateur
WHERE REGEXP_LIKE(NomUtilisateur, '^(SAnderso|SAnderso.*[0-9])$')
This is based on your attempt where you used % in LIKE so that there may be characters following "SAnderso" before the digit(s). If you only want "SAnderso" optionally followed by digits then you would change the regex pattern to ^SAnderso[0-9]*$
thanks to #mhawke who helped me find about REGEX in sql database the solution was to use it but in maria db REGEXP_LIKE does'nt exist so I looked on the documentation and foudn out I could do it like this:
SELECT * FROM Utilisateur WHERE NomUtilisateur REGEXP '^(SAnderso|SAnderso.*[0-9])$'
What can I use instead of LIKE in the queries?
As LIKE has poor performance impact on the search condition.
My scenario:
SELECT
*
FROM
MetaDataTag
WHERE
Name LIKE 'SUN RCC%'
I cant use contains as a Full text index is required on the table, which I am not opting for.
Suggestions will be very helpful.
In your particular case, LIKE is not a bad option because you are only using a postfix wildcard. Your query can actually use an index on the Name column.
Have a look at this visual explanation why it works: http://use-the-index-luke.com/sql/where-clause/searching-for-ranges/like-performance-tuning
try this..
SELECT
*
FROM
MetaDataTag
WHERE PATINDEX('SUN RCC%', Name ) != 0
For some reason I cannot find the answer on Google! But with the SQL contains function how can I tell it to start at the beginning of a string, I.e I am looking for the full-text equivalent to
LIKE 'some_term%'.
I know I can use like, but since I already have the full-text index set up, AND the table is expected to have thousands of rows, I would prefer to use Contains.
Thanks!
You want something like this:
Rather than specify multiple terms, you can use a 'prefix term' if the
terms begin with the same characters. To use a prefix term, specify
the beginning characters, then add an asterisk (*) wildcard to the end
of the term. Enclose the prefix term in double quotes. The following
statement returns the same results as the previous one.
-- Search for all terms that begin with 'storm'
SELECT StormID, StormHead, StormBody FROM StormyWeather
WHERE CONTAINS(StormHead, '"storm*"')
http://www.simple-talk.com/sql/learn-sql-server/full-text-indexing-workbench/
You can use CONTAINS with a LIKE subquery for matching only a start:
SELECT *
FROM (
SELECT *
FROM myTable WHERE CONTAINS('"Alice in wonderland"')
) AS S1
WHERE S1.edition LIKE 'Alice in wonderland%'
This way, the slow LIKE query will be run against a smaller set
The only solution I can think of it to actually prepend a unique word to the beginning of every field in the table.
e.g. Update every row so that 'xfirstword ' appears at the start of the text (e.g. Field1). Then you can search for CONTAINS(Field1, 'NEAR ((xfirstword, "TERM*"),0)')
Pretty crappy solution, especially as we know that the full text index stores the actual position of each word in the text (see this link for details: http://msdn.microsoft.com/en-us/library/ms142551.aspx)
I am facing the similar issue. This is what I have implemented as a work around.
I have made another table and pulled only the rows like 'some_term%'.
Now, on this new table I have implemented the FullText search.
Please do inform me if you tried some other better approach
I need to filter out records based on some text matching in nvarchar(1000) column.
Table has more than 400 thousands records and growing. For now, I am using Like condition:-
SELECT
*
FROM
table_01
WHERE
Text like '%A1%'
OR Text like '%B1%'
OR Text like '%C1%'
OR Text like '%D1%'
Is there any preferred work around?
SELECT
*
FROM
table_01
WHERE
Text like '%[A-Z]1%'
This will check if the texts contains A1, B1, C1, D1, ...
Reference to using the Like Condition in SQL Server
You can try the following if you know the exact position of your sub string:
SELECT
*
FROM
table_01
WHERE
SUBSTRING(Text,1,2) in ('B1','C1','D1')
Have a look at LIKE on msdn.
You could reduce the number filters by combining more details into a single LIKE clause.
SELECT
*
FROM
table_01
WHERE
Text like '%[ABCD]1%'
If you can create a FULLTEXT INDEX on that column of your table (that assumes a lot of research on performance and space), then you are probably going to see a big improvement on performance on text matching. You can go to this link to see what FULLTEXT SEARCH is
and this link to see how to create a FULLTEXT INDEX.
I needed to do this so that I could allow two different databases in a filter for the DatabaseName column in an SQL Server Profiler Trace Template.
All you can do is fill in the body of a Like clause.
Using the reference in John Hartscock's answer, I found out that the like clause uses a sort of limited regex pattern.
For the OP's scenario, MSMS has the solution.
Assuming I want databases ABCOne, ABCTwo, and ABCThree, I come up with what is essentially independent whitelists for each character:
Like ABC[OTT][NWH][EOR]%
Which is easily extensible to any set of strings. It won't be ironclad, that last pattern would also match ABCOwe, ABCTnr, or ABCOneHippotamus, but if you're filtering a limited set of possible values there's a good chance you can make it work.
You could alternatively use the [^] operator to present a blacklist of unacceptable characters.
I want to get all the records from the one table which contain at least one word from the input string. For example, input parameter='Stack over flow':
select *
from sample
where name like '%stack%'
or name like '%over%'
or name like '%flow%'
How do I search for records which contains 'stack' or 'over' or 'flow'?
The code you gave should work. But don't use it. It won't be able to use any indexes, and so will likely be very slow. Look into a full text index and the CONTAINS keyword instead.