I am familiar with this kind of query:
select * from tableA where foo like '%bar%'
But today I run into three adjacent percentage signs in some legacy code, like this:
select * from tableA where foo like '%%%'
This query seems to work, both on mssql and oracle, when foo is of string type (varchar, etc.) but it fails when foo is numeric.
Any idea what it means?
EDIT: sorry about the typo in original question, the query uses the LIKE operator.
Turns out in mysql it matches everthing:
mysql> create database foo;
Query OK, 1 row affected (0.06 sec)
mysql> use foo;
Database changed
mysql> create table foo ( bar char(20) );
Query OK, 0 rows affected (0.06 sec)
mysql> desc foo;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| bar | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> insert into foo values ('endwith%');
Query OK, 1 row affected (0.05 sec)
mysql> insert into foo values ('%startwith');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values ('cont%ins');
Query OK, 1 row affected (0.00 sec)
mysql> insert into foo values ('doesnotcontain');
Query OK, 1 row affected (0.00 sec)
mysql> select * from foo where bar like '%%%';
+----------------+
| bar |
+----------------+
| endwith% |
| %startwith |
| cont%ins |
| doesnotcontain |
+----------------+
4 rows in set (0.00 sec)
If you're trying to find any value with a percent in it you need to use ESCAPE:
e.g.
SELECT *
FROM SomeTable
WHERE foo LIKE '%|%%' ESCAPE '|'
If foo is numeric (in datatype), then you will get an error if you try to compare a numeric value in the column with a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With