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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.