Monday, May 4, 2009

Searching Data in SQL and VB using LIKE

For example 6.6 in chapter 6 of your textbook, the SQL statement allows you to search for users from a specific city:
  • SELECT CustomerID, ..., Fax From Customers WHERE City = @City
Thus if you search "london", you can see those customers from london. However, if you enter "lon", no customers will be returned as there is no city named exactly as "lon".

you can modify the SQL statement to search for data that contains the search string:
  • SELECT CustomerID, ..., Fax From Customers WHERE City LIKE '%' + @City + '%'

If you search for "lon", then customers from "london" and "barcelona" will be returned. The % symbol represents a wildcard character in SQL.

No comments:

Post a Comment