Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Concatenation
Simple text functions
How to modify Strings
Summary

Instruction

Great! You have completed a simple task using LIKE. However, T-SQL and T-SQL Server provide additional ways to use LIKE. One is with square brackets [ ]. You simply put characters in the brackets and place them where these characters would occur in the string.

Look at the following example:

SELECT
  FirstName,
  LastName  
FROM Copywriter
WHERE FirstName LIKE N'[AM]____' ;
A11

This pattern matches all first names that have A or M as the first letter and any of the next 3 letters.

What about a wildcard character that allows you to exclude one of a given set of characters? In this case, you can use ^ as the first character in the square brackets. In this pattern:

N'[^ACH]%'

only strings that do not start with A, C, or H will match.

You can also use [] to indicate a range letters or numbers. For example:

  • N'[0-9]': denotes 0,1,2,3,4,5,6,7,8,9
  • N'[b-f]': denotes b,c,d,e,f

Exercise

Let's find the Ids of items with names that contain at least one digit.

Stuck? Here's a hint!

Use the pattern N'%[0-9]%'.