The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
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
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%'
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'
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%'
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%'
WHERE City NOT LIKE '%tav%'
P_Id | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Hansen | Ola | Timoteivn 10 | Sandnes |
2 | Svendson | Tove | Borgvn 23 | Sandnes |
No comments:
Post a Comment