SQL LIKE operator

Examples LIKE operator

(Designation mode LIKE operator in the WHERE clause for the search column)

Example 1

Now we want to choose people living in cities beginning with "N" from the inside "Persons" table:

We can use the following SELECT statement:

SELECT * FROM Persons WHERE City LIKE 'N%'

Note: "%" may be used to define wildcards (missing letters mode).

Example 2

Next, we want to choose people live in cities with "g" in the end from the "Persons" table:

We can use the following SELECT statement:

SELECT * FROM Persons WHERE City LIKE '%g'

Example 3

Next, we want to choose people live in the city include "lon" from the inside "Persons" table:

We can use the following SELECT statement:

SELECT * FROM Persons WHERE City LIKE '%lon%'

Example 4

By using the NOT keyword, we can choose who live in cities do not include "lon" from the inside "Persons" table:

We can use the following SELECT statement:

SELECT * FROM Persons WHERE City NOT LIKE '%lon%'

 

There is also a wild card "_"

Example 1

Now we want to select the name after the first character from above "Persons" table is "eorge" people:

We can use the following SELECT statement:

SELECT * FROM Persons WHERE FirstName LIKE '_eorge'

Example 2

Next, we want to choose from the "Persons" table surname of this record with "C" at the beginning, then any character, followed by "r", then any character, then "er":

We can use the following SELECT statement:

SELECT * FROM Persons WHERE LastName LIKE 'C_r_er'

Guess you like

Origin www.cnblogs.com/hengzhezou/p/11199373.html