Access: LIKE Condition (using wildcards) in Access 2003/XP/2000/97

 

Access: LIKE Condition (using wildcards) in Access 2003/XP/2000/97


The LIKE condition allows you to use wildcards in the where clause of an SQL statement in in Access 2003/XP/2000/97. This allows you to perform pattern matching. The LIKE condition can be used in any valid SQL statement - select, insert, update, or delete.

The patterns that you can choose from are:

* allows you to match any string of any length (including zero length)

? allows you to match on a single character

# allows you to match on a single numeric digit


For example:

Like 'b*' would return all values that start with b
Like '*b*' would return all values that contain b
Like '*b' would return all values that end with b
Like 'b?' would return all values that start with b and are 2 characters in length
Like 'b#' would return all values that start with b and are 2 characters in length where the second character is a number

Here is an example of how you'd use the LIKE condition in a query:

In this example, we are looking for all company names that start with "b".


Combining the LIKE condition with the NOT operator

You can also combine the LIKE condition with the NOT operator.

For example:

Not Like 'b*' would return all values that do not start with b
Not Like '*b*' would return all values that do not contain b
Not Like '*b' would return all values that do not end with b
Not Like 'b?' would return all values that are not 2 characters in length starting with b
Not Like 'b#' would return all values that are not 2 characters in length that start b and where the second character is a number

Here is an example of how you'd use the NOT LIKE condition in a query:

In this example, we are looking for all company names that do not start with "b".


Frequently Asked Questions


Question:  In Access, I'm trying to create a query that returns phone numbers, but excludes any phone numbers that start with 713612, 713312, or 281999. How can I do this?

Answer:  You can do this by using the NOT LIKE condition.

For example, we have a table called Sample that contains phone numbers within a field called PhNo. We can write a query using the NOT LIKE condition as follows:

The SQL for the query above is:

SELECT Sample.PhNo FROM Sample
WHERE (Sample.PhNo) Not Like '713612*'
  And (Sample.PhNo) Not Like '713312*'
  And (Sample.PhNo) Not Like '281999*';

By separating the NOT LIKE conditions with the AND operator, we are able to exclude all three types of phone numbers from our results.

留言

發佈留言

此網誌的熱門文章

Sales術語