SQL masks can be used when searching for data.
SQL masks can replace one or more characters when searching for data in the database.
SQL masks should be used with the operator SQL LIKE.
In SQL you have the following masks:
| Mask | Description |
|---|---|
| % | Means zero or more characters |
| _ | Means one character |
| [Charlist] | Any of these characters |
| [^ Charlist]
or [! Charlist] |
Any characters other than those |
An example of the use of masks in SQL
There is a table "Persons":
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
| 3 | Pettersen | Kari | Storgt 20 | Stavanger |
Use%
Now we want to select all individuals who live in the city, whose name is begins with the letter "sa" from the table above.
For this we use this query:
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
Now we want to select all individuals who live in the city, whose name is contains a letter "nes". It does not matter in what place will be the characters at the beginning or end of words.
For this we use this query:
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
Use _
Now we want to select all individuals who have a name containing the letters "la" from the table above.
For this we use this query:
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
Now we want to choose people with the surname that starts with the letter "S", then any character, and then "end", then any character, followed by "on" the table "Persons".
For this we use this query:
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
Ispzovanie [Charlist]
Now we want to choose people with the surname, which begins with "b" or "s" or "p" from the table "Persons".
For this we use this query:
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
| 3 | Pettersen | Kari | Storgt 20 | Stavanger |
Now we want to choose people with the surname, which not begins with "b" or "s" or "p" from the table "Persons".
For this we use this query:
Query Result:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
SQL Masks





Comments
SELECT * FROM Persons
WHERE LastName LIKE '[! Bsp]%'
ie she sees the sign "!" as simply a sign. Naturally for a sample of people with the surname that does not begin with "b" or "s" or "p" from the table "Persons", this design works
SELECT * FROM Persons
WHERE LastName NOT LIKE '[Bsp]%' Quote
SELECT * FROM Persons
WHERE LastName NOT LIKE '[bsp]' and, mark! accept as a simple symbol
On the Server Management Studio 2005 rabotaet)))