469,572 Members | 1,307 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Share your developer knowledge by writing an article on Bytes.

WHERE Clause using Wildcards & Pattern Matching

32,198 Expert Mod 16PB
ANSI-89 v ANSI-92
Before we get into all the various types of pattern matching that can be used, there are two ANSI standards used for the main types of wildcard matching (matching zero or more characters or simply matching a single character) :
ANSI-89 - Mainly used only by Jet / ACE SQL
ANSI-92 - Mainly used by SQL Server and other grown-up products
In the later versions of Access it is now possible to select ANSI-92 compatibility as an option, instead of the ANSI-89 compatibility.

Using Wildcard Characters in String Comparisons
Built-in pattern matching provides a versatile tool for making string comparisons. The following table shows the wildcard characters you can use with the Like operator and the number of digits or strings they match.

Pattern Match Character(s) in expressions :
Any single character - '?' [ANSI-89] or '_' (underscore) [ANSI-92]
Zero or more characters - '*' [ANSI-89] or '%' [ANSI-92]
Any single digit (0 - 9) - '#'
Any single character found within charlist - [charlist]
Any single character not found within charlist - [!charlist]

You can use a group of one or more characters (charlist) enclosed in brackets ([ ]) to match any single character in an expression, and charlist can include almost any characters in the ANSI character set, including digits. You can use the special characters opening bracket ([ ), question mark (?), underscore (_), asterisk (*), percent (%) and number sign (#) to match themselves directly only if enclosed in brackets. You cannot use the closing bracket (]) within a group to match itself, but you can use it outside a group as an individual character.

In addition to a simple list of characters enclosed in brackets, charlist can specify a range of characters by using a hyphen (-) to separate the upper and lower bounds of the range. For example, using [A-Z] in a pattern results in a match if the corresponding character position in the expression contains any of the uppercase letters in the range A through Z. You can include multiple ranges within the brackets without delimiting the ranges. For example, [a-zA-Z0-9] matches any alphanumeric character.

It is important to note that the ANSI-92 SQL wildcards (%) and (_) are only available with Microsoft Jet version 4.X and the Microsoft OLE DB Provider for Jet. They will be treated as literals if used through Microsoft Access or DAO.

Other important rules for pattern matching include the following :

An exclamation mark (!) at the beginning of charlist means that a match is made if any character except those in charlist are found in the expression. When used outside brackets, the exclamation mark matches itself.
You can use the hyphen (-) either at the beginning (after an exclamation mark if one is used) or at the end of charlist to match itself. In any other location, the hyphen identifies a range of ANSI characters.
When you specify a range of characters, the characters must appear in ascending sort order (A-Z or 0-100). [A-Z] is a valid pattern, but [Z-A] is not.
The character sequence [] is ignored; it is considered to be a zero-length string ("").
Jul 13 '07 #1
0 9926

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

100 posts views Thread by Roose | last post: by
13 posts views Thread by Roy Hills | last post: by
6 posts views Thread by vpuvvada | last post: by
83 posts views Thread by sunny | last post: by
9 posts views Thread by emagzz | last post: by
127 posts views Thread by bz800k | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.