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

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.

Related

Is "LIKE %%" identical to "LIKE %"?

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 :

MySQL command to get first letter of last name

Hello I have made a dummy table that I am practicing with and I am trying to get the lasts name first letter for example. Aba Kadabra and Alfa Kadabra the last letter of their last name is 'K' so when I was testing some queries such as...
select * from employees
where full_name like 'K%'
select * from employees
where full_name like 'K%'
Neither of these worked. Can anyone tell me the best way to accomplish this?
Because % works that way. See here
So, 'K%' just brings all full_name that start with K.
and '%K' brings all full_name that end with K.
What you need is '% K%', test it please.
MySQL LIKE operator checks whether a specific character string matches
a specified pattern.
The LIKE operator does a pattern matching comparison. The operand to
the right of the LIKE operator contains the pattern and the left hand
operand contains the string to match against the pattern. A percent
symbol ("%") in the LIKE pattern matches any sequence of zero or more
characters in the string. An underscore ("_") in the LIKE pattern
matches any single character in the string. Any other character
matches itself or its lower/upper case equivalent (i.e.
case-insensitive matching). (A bug: SQLite only understands
upper/lower case for ASCII characters by default. The LIKE operator is
case sensitive by default for unicode characters that are beyond the
ASCII range. For example, the expression 'a' LIKE 'A' is TRUE but 'æ'
LIKE 'Æ' is FALSE.)
You can use below query:
select * from table where full_name like '% K%'

Why is the regular expression setminus operator not working for MySQL?

I am trying to select all client names without vowels from a table (should therefore return an empty list) using the setminus operator with regular expressions, but it is simply returning the entire column. The same happens if I try to select all client names without 'a' or 'e' or any other vowel.
This is the query I'm using:
select client_name from client
where client_name regexp '[^aeiou]';
If I try doing a condition like below, then the inside caret actually does take every character other than 'a'. I'm not sure why it doesn't work by itself though.
select client_name from client
where client_name regexp '^[^a]'
Expected - empty output
Actual Results - whole column is returned
The regular expression can match anywhere in the name. So it will match any name that has any non-vowel character, not where all the characters are not vowels. You need to anchor it and quantify it:
WHERE client_name REGEXP '^[^aeiou]*$'
This tests all the characters in the name.
Or you can negate the test:
WHERE client_name NOT REGEXP '[aeiou]'
The regexp matches a vowel anywhere in the name. Then using NOT makes this return the names that don't match.

Can anyone tell me in mysql How to display employee names whose name DO NOT start with alphabet A?

I am a beginner so please help me.
There are 2 things you need to combine in this case.
Because you didn't provide enough information in your question we have to guess what you mean by name. I'm going to assume that you have a single name column, but that would be unusual.
With strings, to match a character column that is not an exact match, you need to use LIKE which allows for wildcards.
You also need to negate the match, or in other words show things that are NOT (something).
First to match names that START with 'A'.
SELECT * FROM table_name WHERE name LIKE 'A%';
This should get you all the PEOPLE who have names that "Start with A".
Some databases are case sensitive. I'm not going to deal with that issue. If you were using MySQL that is not an issue. Case sensitivity is not universal. In some RDBMS like Oracle you have to take some steps to deal with mixed case in a column.
Now to deal with what you actually want, which is NOT (starting with A).
SELECT * FROM table_name WHERE name NOT LIKE 'A%';
your question should have more detail however you can use the substr function
SELECT name FROM yourtable
WHERE SUBSTR(name,1,1) <> 'A'
complete list of mysql string functions here
mysql docs
NOT REGXP operator
MySQL NOT REGXP is used to perform a pattern match of a string expression expr against a pattern pat. The pattern can be an extended regular expression.
Syntax:
expr NOT REGEXP pat
Query:
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^[a]';
or
SELECT * FROM emp_table WHERE emp_name NOT REGEXP '^a';

Parameterized string and wildcards in MySQL

I'm trying to build a MySQL query for a database search operation, where a user can specify a text string to match against a particular column. I figured that using the LIKE operator and surrounding the user input with % signs, to act as wildcards, would be best practice. I want the wildcards to be there on both the start and end so the user does not have to enter the whole string. Furthermore, I'd like to parameterize the query to avoid injection and whatnot. This leaves me with a query that looks something like this:
SELECT * FROM `sometable`
WHERE `name` LIKE ?
ORDER BY `id` ASC
LIMIT 1,10
(Note that the name column is a VARCHAR(50) with collation utf8_general_ci.)
The parameter from the WHERE clause is added like so:
Using cmd As New OdbcCommand()
cmd.Parameters.AddWithValue("name", "%" & strUserInput & "%")
...
However, what I now ended up with appears to be MySQL actually matching the name column against the concatenated string, treating the %'s as literals and not as wildcards as I had intended.
I also tried LIKE CONCAT('%', ?, '%'), but this doesn't work either.
How would I glue a wildcard character to the start and end of a parameterized string? Or is there a much better way of doing this?
Your SqlParameter name is #name not name : cmd.Parameters.AddWithValue("#name", string.Format("%{0}%", strUserInput);
And your sql should be:
SELECT * FROM `sometable`
WHERE `name` LIKE #name
ORDER BY `id` ASC
LIMIT 1,10
Apparently I oversaw something really stupid. The LIMIT clause was the problem - the database I was testing with was supposed to return only one or two rows per query, but because I specified 1,10 it skips the first result. I did not know LIMIT is zero-based. Oops.
Learning oppurtunity, I guess.