Is "LIKE %%" identical to "LIKE %"? - mysql

Are there any differences between the following two queries:
SELECT name from Roster WHERE id LIKE %
and:
SELECT name from Roster WHERE id LIKE %%?

They are equivalent. The % wildcard matches zero or more characters. So both of these check that id is not NULL.
Note that the like pattern needs to be a string. So, both your example would generate syntax errors. You want:
where id like '%'
where id like '%%'
Also, if id is a number, then you should not be using a string operation on it -- under most circumstances.

Here you will get clarification :

Related

Cannot check if a field contains also number

I'm trying to check if a field in a specific table contains also number, in particular I have a record that have the field name which contains this value: Besëlidhja Lezhë vs. Tërbuni Pukë 1 - 1, so I'm trying to get also all the rows of that table that contains a number inside the field name. I tried:
SELECT * FROM `venue` where `name` like '%[0-9]%'
but this will return an empty result, any idea?
This should tell you if name contains any digit (not tested)
SELECT * FROM venue WHERE name REGEXP '[0-9]'
You can try using a Regular Expression that filters for names in your name column with numeric characters . For example:
SELECT * FROM DATA WHERE name REGEXP '[a-z]...[0-9]';
mySQL allows you to use regular expression as a filter !
This should select out for names like Tërbuni Pukë 1 - 1. If you want to practice regular expressions this is a great website to test whether you have the right regex. https://regex101.com/
Hope this helps !
I believe this should work:
SELECT *
FROM venue
WHERE name like '%0%' or name like '%1%' or name like '%2%' or name like '%3%'
and so on til you get to 9. I hope this helps

How does the Like operator behave with an empty argument (%%)?

Using the like operator
SELECT first_name, last_name FROM student_details WHERE first_name LIKE 'S%';
will provide the name which start with S.
But
SELECT first_name, last_name FROM student_details WHERE **first_name LIKE '%%';**
%% - It didn't have any string constraints. This query is returning the complete list.
How is LIKE %% processed in SQL?
Can anyone clarify this?
The % sign is considered the zero-or-more repetition wildcard character in SQL. The way it works is pretty much the same as in any other areas of computing, you can have a wide definition and explaination of how it works just putting it in Google.
That's why it returns all the entries in your database, because it matches anything even if it isn't repeated a time (by the way, the same behavior you'll achieve with LIKE '%').
The one and just one repetition wildcard in SQL is usually represented with _.
If do not pass any search string then LIKE will return all values
You have to see this link
The % signifies zero or more occurences of whatever characters.
So first_name like '%%' (or first_name like '%' for that matter) will return all records where first_name contains zero or more characters. That means all records, except those where first_name is NULL.

wildcard for single digit mysql

I want to use the LIKE operator to match possible values in a column.
If the value begins with "CU" followed by a digit (e.g. "3") followed by anything else, I would like to return it. There only seems to be a wildcard for any single character using underscore, however I need to make sure it is a digit and not a-z.
I have tried these to no avail:
select name from table1 where name like 'CU[0-9]%'
select name from table1 where name like 'CU#%'
Preferably this could be case sensitive i.e. if cu or Cu or cU then this would not be a match.
You need to use regexp:
select name
from table1
where name regexp binary '^CU[0-9]'
The documentation for regexp is here.
EDIT: binary is required to ensure case-sensitive matching
The like operator only have the % and _ wildcards in MySQL, but you can use a regular expression with the rlike operator:
select name from table1 where name rlike '^CU[0-9]'
You can use REGEXP operator, see http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
so your query would be:
select name from table where name regexp 'CU[0-9].*';
Have you tried with:
select name from table where name between 'CU0' and 'CU9'

Need MySQL idiom for checking whether string contains any of a set of characters

I need to run the following MySQL query:
SELECT id, name FROM mytable
WHERE NOT CONTAINS_ANY(name, ';.<>#$!');
except that there doesn't seem to be anything like CONTAINS_ANY (taking two string and checking whether the first string contains any character of those in the second string). What can I do instead? I would like to avoid
SELECT id, name FROM mytable
WHERE name NOT LIKE '%;%'
AND name NOT LIKE '%.%'
AND etc. etc.
and similar ugliness.
Use regex:
SELECT id, name
FROM mytable
WHERE name not rlike '[;.<>#$!]';

How to search using an sql statement?

How can I find all the ids of an SQL table? For example I want to search for the word "key" and return the ids in which this word was found.
Assuming that id is the name of a column in your table you would need to use LIKE
SELECT id
FROM YourTable
WHERE id LIKE '%key%'
The % is a wildcard meaning match any set of zero or more characters so this would find rows where the id value contains the substring (not necessarily word) "key".
SELECT [id field name] FROM [Tabel Name] WHERE [field to seach in for key] LIKE '%key%';
Would this be what you are looking for?
INSERT INTO dest_table (dest_id)
SELECT source_id
FROM source_table
WHERE source_column LIKE '%key%';