TOP

SQL: LIKE Searches

There is no pause in there, “LIKE!…searches!” This is a no teeny-bopper zone (unless you want to program stuff, then by all means)! When you know only part of what you’re looking for in a SQL SELECT statement, especially for AJAX auto-complete queries, you will need to employ the all-consuming power of the “LIKE Search” also known as the “Contains Search” by minds whom SQL eats for breakfast (sorry if that’s you).

Regular SELECT query that finds an exact result:

     SELECT * FROM Customers WHERE CustomerName = 'Microsoft'

I guess you would dub this a literal search. This produces one result: Microsoft

A SELECT query that finds a bunch of matching but ambiguous (kinda fuzzy, hazy) results:

     SELECT * FROM Customers WHERE CustomerName LIKE 'Micro%'

This is more of a wider search for possibilities. This produces more results: MicroCenter, MicroSoft, MicroDerm Abrasionists, MicroBats, MicroMachines, inc. See how this would be helpful in an AJAX auto-complete?

The magic in the LIKE search is that % (percent sign). It tells SQL to give me anything after my “Micro.” As you can see above, it brings you a bunch of search results like a dog with way too many bones. You can also use LIKE ‘%Micro%’ to find things with stuff on both ends. Sometimes this is way more efficient and useful.

     SELECT * FROM Customers WHERE CustomerName LIKE '%Micro%'

Produces aMicrowave, Microsoft, MicroCenter, armpitMicro, etc.

Armpit? Do you see what just happened there, wow, it must be late or something.

  • RSS
  • email
  • Facebook
  • Twitter
  • del.icio.us

Leave a Reply