SQL Query Multiple LIKE - mysql

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.

Related

MySQL underscore with LIKE Operator

I have an SQL query which matches results using a LIKE :
_column_name_%
This will return results where the column name is:
_column_name_1
_column_name_2
The end number could be a really high number e.g. 32523, or even 523624366234.
If I use _column_name_%%%%% this would match 32523, but not 523624366234.
How would I get the LIKE to match without typing % repeatedly?
A Simple select query with the LIKE operator should look like this
You have to escape the underscore using "\" if you are having any.
instead of pretext_% use pretext\_%
Select * from mytable where field_1 like 'pretext\_%'
This will return pretext_1 as well as pretext_11

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.

Doing partial match on name fields in SQL Server

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

MySql, search with LIKE %str%

I search my table with query contains LIKE clause %str%.
Is here a way to know where string 'str' was finded in sentence?
I would like to print out 'str' as markup (bold).
For this I need information where exact 'str' begins in any row which contain 'str'.
you can get the string position using the POSITION function ( http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_position ) however thats probably not the best way to do it since if they use the markup more than once it will only return the first position. It would be easier just to replace the string with the string wrapped with whatever markup you want.
If you want an all MySQL solution this would probably work:
SELECT REPLACE(exampleTable.field, 'search_string', '<b>search_string</b>')
FROM exampleTable
WHERE exampleTable.field LIKE '%search_string%';
However i would recommend doing any replacement like this on the PHP / ASP side... using string replacement tools from the respective language.
Sure, you want INSTR() .
You could also use it in your where clause, though you'd want to compare performance between that and LIKE
SELECT INSTR(`field`, 'str') FROM `table` WHERE 0 < INSTR(`field`, 'str')
Remember that INSTR() returns a 1-based index, that is, the first character is postion 1, not position 0; saving 0 for "not found".

MySQL query - select postcode matches

I need to make a selection based on the first 2 characters of a field, so for example
SELECT * from table WHERE postcode LIKE 'rh%'
But this would select any record that contains those 2 characters at any point in the "postcode" field right? I am in need of a query that just selects the first 2 characters. Any pointerS?
Thanks
Your query is correct. It searches for postcodes starting with "rh".
In contrast, if you wanted to search for postcodes containing the string "rh" anywhere in the field, you would write:
SELECT * from table WHERE postcode LIKE '%rh%'
Edit:
To answer your comment, you can use either or both % and _ for relatively simple searches. As you have noticed already, % matches any number of characters whereas _ matches a single character.
So, in order to match postcodes starting with "RHx " (where x is any character) your query would be:
SELECT * from table WHERE postcode LIKE 'RH_ %'
(mind the space after _). For more complex search patterns, you need to read about regular expressions.
Further reading:
http://dev.mysql.com/doc/refman/5.1/en/pattern-matching.html
http://dev.mysql.com/doc/refman/5.1/en/regexp.html
LIKE '%rh%' will return all rows with 'rh' anywhere
LIKE 'rh%' will return all rows with 'rh' at the beginning
LIKE '%rh' will return all rows with 'rh' at the end.
If you want to get only first two characters 'rh', use MySQL SUBSTR() function
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_substr
Dave, your way seems correct to me (and works on my test data). Using a leading % as well will match anywhere in the string which obviously isn't desirable when dealing with postcodes.