" /> Password <br /><input type="password" name="passwd" id="passwd" class="inputbox" size="18" alt="password" />
Home SQL Masks
PDF

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:

MaskDescription
% 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_IdLastNameFirstNameAddressCity
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:

1
2
SELECT * FROM Persons
WHERE City LIKE 'sa%'

Query Result:

P_IdLastNameFirstNameAddressCity
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:

1
2
SELECT * FROM Persons
WHERE City LIKE '%nes%'

Query Result:

P_IdLastNameFirstNameAddressCity
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:

1
2
SELECT * FROM Persons
WHERE FirstName LIKE '_la'

Query Result:

P_IdLastNameFirstNameAddressCity
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:

1
2
SELECT * FROM Persons
WHERE LastName LIKE 'S_end_on'

Query Result:

P_IdLastNameFirstNameAddressCity
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:

1
2
SELECT * FROM Persons
WHERE LastName LIKE '[Bsp]%'

Query Result:

P_IdLastNameFirstNameAddressCity
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:

1
2
SELECT * FROM Persons
WHERE LastName LIKE '[! Bsp]%'

Query Result:

P_IdLastNameFirstNameAddressCity
1 Hansen Ola Timoteivn 10 Sandnes

Comments

 
+13 #1 Алексей 2009-09-09 17:43 In MS SQL 2000, this design does not work
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
 
 
+5 #2 villian 2010-06-09 11:33 а '[^ Ex]% ' and likewise not rabotaet? Quote
 
 
0 #3 Шелест Константин 2010-10-29 18:22 On the SQL Server Management Studio 2005 is also running the following code:

SELECT * FROM Persons
WHERE LastName NOT LIKE '[bsp]' and, mark! accept as a simple symbol
Quote
 
 
0 #4 Шелест Константин 2010-10-29 18:27
а '[^ Ex]% ' and likewise not rabotaet?


On the Server Management Studio 2005 rabotaet)))
Quote
 
 
0 #5 Antonio 2012-04-16 17:48










Quote
 

Authorization

Nice Ajax Poll

Which one of my extensions is the best?

Statistics

Advertisement