Home > SQL > SQL Like Operator

SQL Like Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

SYNTAX

SELECT column_name(s)

FROM table_name

WHERE column_name LIKE pattern

EXAMPLES

1. Say we want to select the persons living in a city that starts with “s” from the table above.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City LIKE ‘s%’

The “%” sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.

2. Next, we want to select the persons living in a city that ends with an “s” from the “Persons” table.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City LIKE ‘%s’

3. Next, we want to select the persons living in a city that contains the pattern “tav” from the “Persons” table.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City LIKE ‘%tav%’

4. It is also possible to select the persons living in a city that NOT contains the pattern “tav” from the “Persons” table, by using the NOT keyword.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City NOT LIKE ‘%tav%’

Categories: SQL Tags:
  1. No comments yet.
  1. No trackbacks yet.