How to frame a like query to perform and operation - sql

I have an stored procedure which returns result based on what text i send as search string
I have 4 records with text
1 egg whole raw
2 egg cooked
3 egg fried omelet
4 raw egg and tomatoes
I want to perform a like search such that if i specify ' egg ra ' as my search string in my search text box it should filter and return two rows as result
1 egg whole raw
2 raw egg and tomatoes
how should my like query be which will return me this result ?

select * from your_table
where text_column like '%egg%'
and text_column like '%ra%'
But this won't make use of indexes and can be slow.

select * from your_table WHERE FREETEXT (text_column, "egg ra");
You have to enable full free text searching in MSSQL to do the above, below are the other options used fro searching for a text.
CONTAINS (Transact-SQL)
CONTAINSTABLE (Transact-SQL)
FREETEXT (Transact-SQL)
FREETEXTTABLE (Transact-SQL)
Read this for more info - http://msdn.microsoft.com/en-us/library/ms142547%28v=sql.105%29.aspx

Related

Search Medicine name in pharma industry with few characters from each word in a lengthy medicine name

Product name:
AMOXICILLIN SUSP 250MG/5ML 100ML
AMOXIN SUSP 500MG/5ML 100ML
Client requirement is when they search the product like
"amoxi" or "amo susp" or "amox susp" or "amo sus 250 100"
100 250 sus amo or amox susp**
Result when I send parameter amox susp to the stored procedure will be the above two products as both contain this data.
We need to display all similar product names like "amoxilin 10mg" or "amoxilin 30mg". Basically if the product name is too lengthy he is going to search the word using few characters from the different words in same product. Sometimes even reverse too should work like mentioned in point b. Looking for inputs to achieve this.
Without using full-text search and index would like to achieve this.
The simplest and effective way of performing this search without using full text search is to (1) use the logic on the procedure to split the parameter into "words" and then (2) perform the search using LIKE and OR.
For example, if you have the parameter 100 250 sus amo you can split it into:
100
250
sus
amo
Then the query is:
select *
from t
where lower(description) like lower('%100%')
or lower(description) like lower('%250%')
or lower(description) like lower('%sus%')
or lower(description) like lower('%amo%')

SQL Server Full Text Search to find containing characters

I have a table with a column Document that is FullText Index.
Let say I have this in this table:
| ID | Document |
| 1 | WINTER SUMMER SPRING OTHER |
My requirement is to find rows that contains 'ER'.
For this I am querying like this:
SELECT TOP 100
[FullTextSearch].[Document], [FullTextSearch].[ID]
FROM
[FullTextSearch]
WHERE
CONTAINS(Document, '"*ER*"')
But this is not working.
Please suggest what should be best way to do this using FullTextSearch.
I am expecting id 1 should be returned.
You can user LIKE operator to find the value.
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards used in conjunction with the LIKE operator:
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
Syntax,
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
This query can help to find the result.
SELECT Document,ID FROM FullTextSearch
WHERE Document LIKE '%ER%';
It's a wildcard query...This should work.
SELECT TOP 100
[FullTextSearch].[Document], [FullTextSearch].[ID]
FROM
[FullTextSearch]
WHERE
Document like '%ER%'
========OR=============
SELECT TOP 100
[FullTextSearch].[Document], [FullTextSearch].[ID]
FROM
[FullTextSearch]
WHERE
CONTAINS(Document, '%ER%')

Fulltext search in SQL Server

I'm trying to create a simple search page on my site but finding it difficult to get full text search working as I would expect it to word.
Example search:
select *
from Movies
where contains(Name, '"Movie*" and "2*"')
Example data:
Id Name
------------------
1 MyMovie
2 MyMovie Part 2
3 MyMovie Part 3
4 MyMovie X2
5 Other Movie
Searches like "movie*" return no results since the term is in the middle of a work.
Searches like "MyMovie*" and "2*" only return MyMovie Part 2 and not MyMovie Part X2
It seems like I could just hack together a dynamic SQL query that will just
and name like '%movie%' and name like '%x2%' and it would work better than full text search which seems odd since it's a large part of SQL but doesn't seem to work as good as a simple like usage.
I've turned off my stop list so the number and single character results appear but it just doesn't seem to work well for what I'm doing which seems rather basic.
select
*
from Movies
where
name like ('%Movie%')
or name like ('%2%')
;
select * from Movies where freetext(Name, ' "Movie*" or "2*" ')

sql full text search not searching exact phrase

I have a database of around 10,000 records. Each record have a text of aprx 40 pages each. I need to implement full-text search in my database coz like query is taking lot of time.
I created the index, following these instructions and tried searching using full-text search.Although it increased the speed of showing results. But I am not able to search phrases in my table.
I am using following query for my exact phrase search
select * from ptcsoftcitation
WHERE CONTAINS(Judgment,'"said contention raised by the counsel"');
It's giving all those results which contains all the words but not exact phrase. It's behaving like ' "said" and "contention" and "raised" and "counsel" '
Please help me..
Assume I have 3 record:
bc
abc
bcd
Search exactly:
= 'bc' will return bc
Search closely:
LIKE 'bc' will return bc
Search contains:
LIKE '%bc' will return bc and abc
LIKE 'bc%' will return bc and bcd
LIKE '%bc%' will return bc, abc and bcd
I think your code should:
SELECT * FROM ptcsoftcitation
WHERE Judgment LIKE '%said contention raised by the counsel%'

How to use Like '%' in a phrase with multiple words

Let's say i have the following table
user|text
1 |red 123 orange blue green
2 |red orange blue
3 |blue orange 123 red
If I wanted to pull all users whose text includes both '123' and 'blue', how would i do it? I would want to pull user 1 and 3.
SELECT *
FROM Table
WHERE text LIKE '%123%'&&'%blue%'
OR text LIKE '%blue%'&&'%123%'
Is this better solved thru using a regexp function?
Try this code:
SELECT *
FROM Table
WHERE text LIKE '%123%'
and text like '%blue%'
You need to repeat LIKE for each pattern.
SELECT *
FROM Table
WHERE text LIKE '%123%' AND text LIKE '%blue%'
You could also write it as:
WHERE text LIKE '%123%blue%' OR text LIKE '%blue%123%'
or:
WHERE text RLIKE '123.*blue|blue.*123'
However, these two solutions get exponentially large if you have to match several strings in any order. The first version is linear in the number of matches.