Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double %% in where clause?

I have a where clause like the below example:

WHERE (subject LIKE 'chef%') AND (dep LIKE 'psy%%')

What is the difference using 1 or 2 percent signs? I know what one means (wild card), but not what the second adds in functionality.

like image 417
JAndersson Avatar asked Apr 27 '15 08:04

JAndersson


People also ask

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Can we use 2 conditions in WHERE clause?

You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on the values in multiple columns. You can use the AND and OR operators to combine two or more conditions into a compound condition.

When the wildcard in a WHERE clause is useful?

Solution: The wildcard in a WHERE clause is useful when an exact match is not possible in a SELECT statement.

Can WHERE clause have multiple values?

The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.


2 Answers

That query is likely a typo; putting a double % does nothing in the example you have given as there are no non-wildcard characters between the two %'s.

If you wanted to search for an actual % character you can use escaping.

like image 80
melodiouscode Avatar answered Sep 29 '22 10:09

melodiouscode


Usage of double '%' is mostly for specifying there are no wildcards present between them.however it is also used for using actual '%' in searchquery which is called escaping.

using '%' escaping looks like

SELECT name FROM emp 
   WHERE id LIKE '%\%%' ESCAPE '\'

for more details on escaping Escaping

like image 41
akhil kumar Avatar answered Sep 29 '22 08:09

akhil kumar