The LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.SQL LIKE Syntax
SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern |
LIKE Operator Example
The "Persons" table:P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Timoteivn 10 | Sandnes |
2 | Svendson | Tove | Borgvn 23 | Sandnes |
3 | Pettersen | Kari | Storgt 20 | Stavanger |
We use the following SELECT statement:
SELECT * FROM Persons WHERE City LIKE 's%' |
The result-set will look like this:
P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Timoteivn 10 | Sandnes |
2 | Svendson | Tove | Borgvn 23 | Sandnes |
3 | Pettersen | Kari | Storgt 20 | Stavanger |
We use the following SELECT statement:
SELECT * FROM Persons WHERE City LIKE '%s' |
P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Timoteivn 10 | Sandnes |
2 | Svendson | Tove | Borgvn 23 | Sandnes |
We use the following SELECT statement:
SELECT * FROM Persons WHERE City LIKE '%tav%' |
P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
3 | Pettersen | Kari | Storgt 20 | Stavanger |
We use the following SELECT statement:
SELECT * FROM Persons WHERE City NOT LIKE '%tav%' |
P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Timoteivn 10 | Sandnes |
2 | Svendson | Tove | Borgvn 23 | Sandnes |