Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL "like" statement: 'Jeff' LIKE 'Jeff' = false?

Tags:

sql

EDIT: Problem solved. It was my reading that was incorrect, not the SQL :p

Hi!

I was just reading in my database book about the LIKE SQL statement. It said this:

SELECT whatever FROM whereever WHERE 'Jeff' LIKE 'Jeff';

It continued to say that the statement 'Jeff' LIKE 'Jeff' would always return false. The book did not tell me why, nor can I find this anywhere else. Does this then mean that the following SQL also would return null?

SELECT W.name FROM whereever W WHERE w.name LIKE 'Jeff';

For reference, the book is: Database Management Systems: Ramakrishnan - Gehrke From McGRAW - Hill. International edition. ISBN 0-07-123151-X PAGE 140.

like image 852
Automatico Avatar asked Dec 16 '22 14:12

Automatico


2 Answers

I took a look at that page (Amazon "search inside this book") and the key thing that you're missing is that the author is making a point there about whitespace. The book actually says

Thus, 'Jeff'='Jeff ' is true, while 'Jeff' LIKE 'Jeff ' is false.

Note the spaces!

Just to make it clear why the mistake occurred, here's the text:

enter image description here

Since the space is at the end of the line, it's hard to see. But as I say below, the quote mark wouldn't wrap to the next line if there were no space there.

like image 88
Jacob Mattison Avatar answered Dec 19 '22 08:12

Jacob Mattison


MySQL would disagree with your book (not sure about other DBMSes):

mysql> SELECT 'Jeff' LIKE 'Jeff';
+--------------------+
| 'Jeff' LIKE 'Jeff' |
+--------------------+
|                  1 |
+--------------------+
1 row in set (0.00 sec)
like image 44
Chris Morgan Avatar answered Dec 19 '22 07:12

Chris Morgan