Books
Books

Reputation: 5323

Wild card search in SQL

I have ran across a few queries that use this where condition:

select ...
where Name like '%'

Is there a purpose for using a wildcard like that?

Upvotes: 3

Views: 390

Answers (2)

Seb Wiers
Seb Wiers

Reputation: 66

WHERE Name LIKE '%' is equivalent to WHERE Name IS NOT NULL (at least in the tests I ran). The later is perhaps more efficient, and IMO much easier to see the intent of, but I suppose like '%' saves few keystrokes. Either way, its not just filler.

Upvotes: 5

webjprgm
webjprgm

Reputation: 4571

Probably not, unles there's some edge case I don't know about with the way a text search is handled. I'd do an experiment to see what it does if Name is null if I were near a MySQL client now.

I've seen some code that uses queries with "...where 1=1" just because it was easier to output a meaningless where clause than to deal with cases where a where clause is absent in generated queries. Or I there used to be a meaningful condition that was removed in some edit.

Upvotes: 1

Related Questions