1. Learn
  2. /
  3. Courses
  4. /
  5. Functions for Manipulating Data in SQL Server

Exercise

Looking for a pattern within a string

If you want to search for a pattern in a string, PATINDEX() is the function you are looking for. This function returns the starting position of the first occurrence of the pattern within the string.

The syntax is: PATINDEX('%pattern%', expression)

pattern match
% any string of zero or more characters
_ any single character
[] any single character within the range specified in brackets

In this exercise, you are going to use the voters table to look at information about the voters whose names follow a specified pattern.

Instructions 1/4

undefined XP
  • 1

    Write a query to select the voters whose first name contains the letters "rr".

  • 2

    Write a query to select the voters whose first name starts with "C" and has "r" as the third letter.

  • 3

    Select the voters whose first name contains an "a" followed by other letters, then a "w", followed by other letters.

  • 4

    Write a query to select the voters whose first name contains one of these letters: "x", "w" or "q".