Wildcard Searches with LIKE

So far, you have been able to do simple string comparisons. But when working with strings, you will often need to search patterns within strings. To do that, you will need wildcard characters.

SQL supports two basic wildcard characters:

  • The percent sign (%) stands for any number of characters (or no characters).

  • The underscore (_) stands for any single character.

Let's see a few examples.

In an earlier query, we searched for the value 'Tofu' with an equality condition. Let's repeat that query, but change the condition to look for anything starting with 'T'. I'll use the % wildcard and the LIKE keyword:

select ProductName, UnitPrice
  from Products
 where ProductName like 'T%'

Results:

 ProductName UnitPrice ---------------------------------------- ...

Get Sams Teach Yourself Transact-SQL in 21 Days, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.