views:

46

answers:

2
Will the following query evaluate to true (1), false (0), or NULL?

SELECT '%' LIKE ' % ';

the answer provided is

The '%' character is matched by '%', but not by the space characters surrounding it, so the expression evaluates to false.

+----------------+
| '%' LIKE ' % ' |
+----------------+
|            0  |
+----------------+

but i thought % can match zero or more characters? so % can match % + Spaces? or does characters nt include wildcards?

UPDATE:

oh but if the comparison happens the other way arnd it is true ... hmm ...

SELECT ' % ' LIKE '%';
Any non-NULL string is matched by the '%' metacharacter, so the expression evaluates to true.

+----------------+
| ' % ' LIKE '%' |
+----------------+
|          1    |
+----------------+
+2  A: 

Logic is wrong. You had to write

select ' % ' like '%'

If you are writing like ' % ', it means that in first string must be space, then any symbols and one more space in the end. Wildcards is for like statement, in first string it's not wildcard but symbol.

x2
A: 

Not entirely sure what your question is, but example time:

Sample table, mytbl:

col1
----
abc
def
feh
zba
a b

Query1
------
select * from mytbl where col1 like '%b%'

Result1
-------
abc
zba
a b

Query2
------
select * from mytbl where col1 like '%b'

Result2
------
a b

Query3
------
select * from mytbl where col1 like 'a%'

Result3
-------
abc
a b

Query4
------
select * from mytbl where col1 like '% b%'

Result4
-------
a b

Query5
------
select * from mytbl where col1 like '% b %'

Result5
-------
null

As you can see, the % matches zero or more characters. Non-% characters are treated as literals. So that means % b % is looking for anything + space + b + space + anything.

Hopefully, this helps.

Eric