SQL WHERE LIKE Statement

The SQL WHERE LIKE syntax

The general syntax is:

  1. SELECT columnnames
  2. FROM tablename
  3. WHERE columnname LIKE value

Optional Wildcard characters allowed in ‘value’ are % (percent) and _ (underscore).
A % matches any string with zero or more characters.
An _ matches any single character.


 

SQL WHERE LIKE Examples

Problem: List all products with names that start with ‘Ca’

  1. SELECT Id, ProductName, UnitPrice, Package
  2. FROM Product
  3. WHERE ProductName LIKE ‘Ca%’

Results: 2 records.

Id ProductName UnitPrice Package
18 Carnarvon Tigers 62.50 16 kg pkg.
60 Camembert Pierrot 34.00 15-300 g rounds

 


Problem: List all products that start with ‘Cha’ or ‘Chan’ and have one more character.

  1. SELECT Id, ProductName, UnitPrice, Package
  2. FROM Product
  3. WHERE ProductName LIKE ‘Cha_’ OR ProductName LIKE ‘Chan_’

Results: 2 records.

Id ProductName UnitPrice Package
1 Chai 18.00 10 boxes x 20 bags
2 Chang 19.00 24 – 12 oz bottles

댓글 남기기