The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.
Like operator
The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.
SQL Like operator syntax
SELECT column_name (s) from Table_namewhere column_name as the original table (used in the example):
Persons table:
Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
Like operator instance
Example 1
Now, we want to select from the "Persons" table above the people who live in the city starting with "N":
We can use the following SELECT statement:
SELECT * from Personswhere city like ' N% ' tip: '% ' can be used to define wildcard characters (missing letters in the pattern).
Result set:
Id LastName FirstName Address City
2 Bush George Fifth Avenue New York
Example 2
Next, we want to select from the "Persons" table the people who live in cities that end with "G":
We can use the following SELECT statement:
SELECT * from Personswhere city like '%g ' result set:
Id LastName FirstName Address City
3 Carter Thomas Changan Street Beijing
Example 3
Next, we want to select from the "Persons" table the people who live in the city that contains "lon":
We can use the following SELECT statement:
SELECT * from Personswhere city like '%lon% ' result set:
Id LastName FirstName Address City
1 Adams John Oxford Street London
Example 4
By using the NOT keyword, we can select people residing in cities that do not contain "lon" from the "Persons" table:
We can use the following SELECT statement:
SELECT * from Personswhere '%lon% ' result set:
Id LastName FirstName Address City
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
Like operator
The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.
SQL Like operator syntax
SELECT column_name (s)
From table_name
WHERE column_name like pattern hint: "%" can be used to define wildcard characters (missing letters in the pattern).
When searching for data in a database tutorial, you can use the SQL wildcard character.
SQL wildcard character
When searching for data in a database, the SQL wildcard character can override one or more characters.
SQL wildcard characters must be used with the LIKE operator.
In SQL, you can use the following wildcard characters:
Wildcard description% replaces one or more characters _ only one character [charlist] Word columns any single character [^charlist] or [!charlist] not Word
The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.
Like operator
The LIKE operator is used to search for a specified pattern in a column in a WHERE clause.
SQL Like operator syntax
SELECT column_name (s) from table_name WHERE column_name like pattern
The original table (in the example):
Persons table: Id LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing
Like operator instance
Example 1
Now, we want to select from the "Persons" table above the people who live in the city starting with "N": we can use the following SELECT statement: SELECT * from Persons WHERE town like ' N% '
Tip: "%" can be used to define wildcard characters (missing letters in the pattern).
Result set:
Id LastName FirstName Address City
2 Bush George Fifth Avenue New York
Example 2
Next, we want to select from the "Persons" table the people who live in cities that end with "G":
We can use the following SELECT statement:
SELECT * from Persons WHERE city like '%g '
Result set:
Id LastName FirstName Address City
3 Carter Thomas Changan Street Beijing