SQL: Using the LIKE Operator

SQL: Using the LIKE Operator

LIKE
Introduction

 

  • LIKE is a special operator just for strings
  • LIKE will match strings with a pattern
Wildcard Character %
Introduction

 

  • You can use the % Wildcard character with LIKE
  • % Wildcard character can go anywhere in the string
  • The percent sign % wildcard character matches any sequence of zero or more characters
  • The underscore _ wildcard character matches any single character.

Example

Who are all the people in my contact list that have a first name that begins with the letter J?
SELECT p.person_first_name

FROM person p WHERE p.person_first_name LIKE ‘J%’;

 

Example Table

Employee

EmpID FirstName LastName City Salary
2458 Nagesh Crasto Brooklyn 10000
4857 Suresh Shetty Denver 20000
5874 Magesh C Buffalo 30000
8578 Yogesh C Austin 40000

 

Example 1:

SELECT FirstName, City, Salary

FROM Employee

WHERE FirstName LIKE ‘%ge%’;

FirstName City Salary
Nagesh Brooklyn 10000
Magesh Buffalo 30000
Yogesh Austin 40000

 

Example 2:

SELECT FirstName, City, Salary

FROM Employee

WHERE FirstName LIKE ‘_ages_’;

FirstName City Salary
Nagesh Brooklyn 10000
Magesh Buffalo 30000
Yogesh Austin 40000

 

Example 3:

SELECT FirstName, City, Salary

FROM Employee

WHERE City LIKE ‘%al’;

FirstName City Salary
Magesh Buffalo 30000

 

Example 4:

SELECT FirstName, City, Salary

FROM Employee

WHERE FirstName LIKE ‘_agesh’;

FirstName City Salary
Nagesh Brooklyn 10000
Magesh Buffalo 30000

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s