Thursday, April 8, 2010

Like Operator

0 comments
1. LIKE with the % wildcard character
The following example finds all telephone numbers that have area code 415 in the Contact table.

SELECT FirstName, LastName, Phone
FROM Person.Contact
WHERE phone LIKE '415%'

The following example finds all telephone numbers that have area last digits 415 in the Contact table.

SELECT FirstName, LastName, Phone
FROM Person.Contact
WHERE phone LIKE '%415'


The following example finds all telephone numbers that have area 415 in the Contact table.

SELECT FirstName, LastName, Phone
FROM Person.Contact
WHERE phone LIKE '%415%'

3. Usng NOT LIKE with the % wildcard character

SELECT FirstName, LastName, Phone
FROM Person.Contact
WHERE Phone NOT LIKE '415%' AND FirstName = 'Gail'ORDER BY LastName;

4. Using the [ ] wildcard characters

The following example finds Contacts with the first name of Cheryl or Sheryl.

SELECT ContactID, FirstName, LastName FROM Person.Contact WHERE FirstName LIKE '[CS]heryl';

The following example finds the rows for Contacts with last names of Zheng or Zhang.

SELECT LastName, FirstName, Phone
FROM Person.ContactWHERE LastName
LIKE 'Zh[ae]ng'ORDER BY LastName ASC, FirstName ASC;

0 comments: