Doing partial match on name fields in SQL Server - sql-server-2008

I need to enable partial matching on name search. Currently it works with Like '%#name%' but it's not good enough.
We need to enable typing in both first name and last name and both need to be partial, so I'm assuming full text is the way to go.
The problem is that I can't get it do a partial match on a name. For example searching for my name (Fedor Hajdu) will work if I type in either parts in full but not partial (It should match a search for 'fe ha' for example.
How can I achieve this? Can fulltext index be set to do something like syllable matching?

humm three options that mya help you:
the INFLECTIONAL form (Link)
CONTAINS with NEAR (Link)
CONTAINS with Weighted Values (Link)
If that doesn't help, get the string 'fe ha' and add a '%' on every blank space and do a Like:
Like '%fe%ha%'

Using CONTAINS() or CONTAINSTABLE() all you need to do is add * at the end of your matching string:
CONTAINS (Description, '"top*"' );
If you have your string as a parameter you may concatenate like this:
SET #SearchTerm = '"' + #NameParameter + '*"'
CONTAINS (Description, SearchTerm );
https://technet.microsoft.com/en-us/library/ms142492(v=sql.105).aspx

Related

Match specific string before user input

I have the following strings:
SDZ420-1241242,
AS42-9639263,
SPF3-2352353
I want to "escape" the SDZ420- part while searching and only search using the last digits, so far I've tried RLIKE '^[a-zA-Z\d-]' which works but I am confused on how to add the next digits (user input, say 1241242) to it. I cannot use LIKE '%$input' since that would return a row even if I just input '242' as the search string.
In simple words, a user input of '1241242' should return the row with 'SDZ420-1241242'. Is there any other approach other than creating a separate table with the numbers only?
Note that without jumping through some crazy hoops, this search needs to hit every row in the table; if you have an index on this, it's not going to use that (an index is generally used, assuming it's of the proper kind, which they tend to be, when you search on start, and generally only when using LIKE 'needle%' and not RLIKE. If that's a problem, storing the digits separately, and then putting an index on that, is probably the simplest way to solve your problem here.
To query for the final few digits, why not:
SELECT * FROM foo WHERE colName LIKE ?
with the string made in your programming language via:
String searchTerm = "%-" + digits;
You can also pass in the number as a string and use:
where substring_index(colname, '-', -1) = ?
This does not require changing the value in the application code.

What is the best way to use MYSQL Search on term not working

What is the best way to search a database for a phrase such as "Almond Anise Cookie" and return the result?
If I
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
and use the phrase "Almond Cookie", nothing is returned as expected. But if I search for "Anise Cookie" the result above is returned.
I've also tried
SELECT *
FROM recipes
WHERE name LIKE '%".$query."%'
OR name LIKE '".$query."%'
OR name LIKE '%".$query."'
with the same failed result.
Using MATCH AGAINST returns everything that contains "Almond" and everything that contains "Cookie" also not a good result. Is there a happy middle in returned results?
You can try using REPLACE. Something like this should work:
SELECT *
FROM recipes
WHERE NAME LIKE REPLACE(' ".$query." ',' ','%');
Note that I purposely add spaces between .$query. to ensure that the replace operation will make your term filled with the wildcard symbol. In the example above:
If $query='almond cookies' then REPLACE(' ".$query." ',' ','%') will become %almond%cookies%.
You can test the fiddle here : https://www.db-fiddle.com/f/kMzp99S8ENbTkYcW5FVdYN/0

SQL Query Multiple LIKE

currently I'm making something.
using (var IDatabaseQuery = Lightningbolt.GetDatabaseManager().CreateQueryObject())
{
IDatabaseQuery.SetQuery("SELECT * FROM catalogue_baseitems WHERE name LIKE '%hc2_%' OR name LIKE '%hc3_';");
data = IDatabaseQuery.FetchTable();
}
That's what I use, I want to get all the items beginning with hc2_ and hc3_, however, I only get the items starting with hc2_. In the SQL contains also items starting with hc3_ but it doesn't show while executing the query. What do I wrong?
If you want items beginning with hc2_ or hc3_, you need to make two changes:
Don't use the % at the beginning, and
Escape the underscore because it masks to "any one character"
Try something like this:
SELECT * FROM catalogue_baseitems WHERE name LIKE 'hc2\_%' OR name LIKE 'hc3\_%'
Note that %hc2_% will match any of the following examples:
abchc2X (because of the leading % and the underscore will match the X)
hc23 (because underscore will match the 3)
... and so on
You are missing the second percent sign: '%hc3_%'
You want result which contains hc2_ and hc3_ at the beginning then you need to use clause like this 'hc2_%' or hc3_%.
You are getting results for hc2_ because you are using '%hc2_%', this return any string which contains hc2_ anywhere in the string.
Change your query to this
IDatabaseQuery.SetQuery("SELECT * FROM catalogue_baseitems WHERE name LIKE 'hc2\_%' OR name LIKE 'hc3\_%';");
and don't forgot about underscore(_), this is a wildcard character.

SQL LIKE wildcard space character

let's say I have a string in which the words are separated by 1 or more spaces and I want to use that string in and SQL LIKE condition. How do I make my SQL and tell it to match 1 or more blank space character in my string? Is there an SQL wildcard that I can use to do that?
Let me know
If you're just looking to get anything with atleast one blank / whitespace then you can do something like the following WHERE myField LIKE '% %'
If your dialect allows it, use SIMILAR TO, which allows for more flexible matching, including the normal regular expression quantifiers '?', '*' and '+', with grouping indicated by '()'
where entry SIMILAR TO 'hello +there'
will match 'hello there' with any number of spaces between the two words.
I guess in MySQL this is
where entry RLIKE 'hello +there'
I know this is late, but I never found a solution to this in relation to a LIKE question.
There is no way to do what you're wanting within a SQL LIKE. What you would have to do is use REGEXP and [[:space:]] inside your expression.
So to find one or more spaces between two words..
WHERE col REGEXP 'firstword[[:space:]]+secondword'
Another way to match for one or more space would be to use [].
It's done like this.
LIKE '%[ ]%'
This will match one or more spaces.
you can't do this using LIKE but what you can do, if you know this condition can exist in your data, is as you're inserting the data into the table, use regular expression matching to detect it up front and set a flag in a different column created for this purpose.
I just replace the whitespace chars with '%'. Lets say I want to do a LIKE query on a string like this 'I want to query this string with a LIKE'
#search_string = 'I want to query this string with a LIKE'
#search_string = ("%"+#search_string+"%").tr(" ", "%")
#my_query = MyTable.find(:all, :conditions => ['my_column LIKE ?', #search_string])
first I add the '%' to the start and end of string with
("%"+#search_string+"%")
and then replace other remaining whitespace chars with '%' like so
.tr(" ", "%")
http://www.techonthenet.com/sql/like.php
The patterns that you can choose from are:
% allows you to match any string of any length (including zero length)
_ allows you to match on a single character
I think that the question is not asking to match any spaces but to match two strings one a pattern and the other with wrong number of spaces because of typos.
In my case I have to check two fields from different tables one preloaded and the other filled typed by users so sometimes they don't respect 100% the pattern.
The solution was to use LIKE in the join
Select table1.field
from table1
left join table2 on table1.field like('%' + replace(table2.field,' ','%')+'%')
if the condition:
WHERE myField LIKE '%Hello world%'
doesn't work try
WHERE myField LIKE '%Hello%'
and
WHERE myField LIKE '%world%'
this approach is helpful in a few specific use cases, hope this helps.

MYSQL SELECT LIKE "masks"

I'm using a field in a table to hold information about varios checkboxes (60).
The field is parsed to a string to something like this
"0,0,0,1,0,1,0,1,..."
Now I want to make a search using a similar string to match the fields. I.e.
"?,?,1,?,?,1,..."
where the "?" means that it must be 0 or 1 (doesn't matter), but the "1" must match.
As i've seen the '%' is somewhat innapropriate for this case, don't?
Obviosly both strings have the same lenght.
Suggestions?
You can use the underscore (_) character to match a single character in the mask.
Taken from MySQL documentation.