Search for Wild Card in SQL

I came across multiple queries that use this when the condition is:

select ...
where Name like '%'

      

Does it make sense to use a wildcard?

+3


source to share


2 answers


WHERE Name LIKE '%'

is equivalent WHERE Name IS NOT NULL

(at least in the tests I ran). The later version is arguably more efficient and IMO much easier to see the intent, but I suppose it like '%'

saves a few keystrokes. Either way, it's not just filler.



+5


source


Probably not, I have some kind of edge case that I am not aware of with how text search is handled. I would do an experiment to see what it does if Name is null if I'm near a MySQL client right now.



I've seen some code that uses queries with "... where 1 = 1" just because it was easier to output a nonsensical where clause than to deal with cases where the where clause is missing from the generated queries. Or I used to be a meaningful condition that has been removed in some editions.

+1


source







All Articles