Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 16

Metacharacters Suppor ted

SQL Server 2000 supports a limited number of metacharacters, some of which have nonstandard usage and meaning. Each of the four metacharacters is used in the context of the LIKE keyword.

The metacharacters supported in SQL Server 2000 are listed in the following table.

Metacharacter

Meaning

 

 

%

Matches zero or more characters. % is not a quantifier.

_

The underscore character matches a single character. It is not a quantifier.

[...]

Matches a character class. Character class ranges are supported.

[^... ]

Matches any character except those in the character class.

 

 

Many aspects of regular expressions are not supported for use with the LIKE keyword. The following table lists regular expressions features that are not supported.

Metacharacter or Functionality

Comment

 

 

\d

Not supported

\w

Not supported

Back references

Not supported

?

Not supported

*

Not supported; the % metacharacter is not a quantifier

+

Not supported

{n,m}

Not supported

Lookahead

Not supported

Using LIKE with Regular Expressions

The LIKE keyword is used in a WHERE clause which, in turn, is part of a SELECT statement. The LIKE keyword allows the WHERE clause to filter on a regular expression pattern, rather than simply on a literal character sequence.

The Try It Out sections that follow look at examples that make use of the limited collection of metacharacters that SQL Server 2000 supports.

The % Metacharacter

The % metacharacter matches zero or more characters. The % metacharacter is equivalent to the metasequence .* in more standard regular expression implementations.

366

Regular Expression Functionality in SQL Server 2000

Try It Out

The % Metacharacter

1.Open Query Analyzer. In Windows XP, select Start All Programs SQL Server Query Analyzer.

2.From the Connect to SQL Server dialog box, connect to the appropriate SQL Server. The Query Analyzer should open with an appearance similar to that shown in Figure 16-1. The appearance may vary depending on previous use of Query Analyzer and option settings.

3.In the first query, you will use the pubs sample database and will select authors whose last name begins with B. You can do this by using the pattern B%, where the metacharacter % matches any number of any character or combination of characters.

Type the following Transact-SQL code into the query pane of the Query Analyzer:

USE pubs

SELECT au_lname, au_fname FROM dbo.authors

WHERE au_lname LIKE ‘B%’

ORDER BY au_lname

Figure 16-1

367

Chapter 16

4.Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-2 shows the appearance after Step 4. Notice that the last name and first name of authors whose surname begins with B are displayed in the results pane, in the lower part of the figure.

If you have mistyped the Transact-SQL code, an error message may appear in the results pane. If you cannot find the error, the code is included in the file BSurnames.sql.

5.To match surnames that contain the letter B, either case, occurring at the beginning of the surname or later, you can use the pattern %b%.

Type the following Transact-SQL code into the query pane of the Query Analyzer:

USE pubs

SELECT au_lname, au_fname FROM dbo.authors

WHERE au_lname LIKE ‘%b%’

ORDER BY au_lname

6.Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-3 shows the appearance. Notice that the last names of the authors in the result set each contain a letter b, either at the beginning of the word or later in the word.

If you have difficulty when you type the code in, you can use the file BAnywhere.sql as an alternative way to run the code.

Figure 16-2

368

Regular Expression Functionality in SQL Server 2000

Figure 16-3

7.To find last names that contain the character sequence nt, you can use the pattern %nt%.

Type the following Transact-SQL code into the query pane of the Query Analyzer:

USE pubs

SELECT au_lname, au_fname FROM dbo.authors

WHERE au_lname LIKE ‘%nt%’

ORDER BY au_lname

8.Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-4 shows the appearance. Notice that each of the surnames contains the character sequence nt.

The file NTanywhere.sql contains the code if you prefer not to type it into the query pane.

9.The LIKE keyword can be used more than once in the same WHERE clause. For example, if you wanted to find authors with surnames that begin with R and a first name that begins with the character sequence Al, you could use the pattern R% in relation to the au_lname column and the pattern Al% in relation to the au_fname column.

369

Chapter 16

Type the following code into the query pane in Query Analyzer:

USE pubs

SELECT au_lname, au_fname FROM dbo.authors

WHERE au_lname LIKE ‘R%’ AND au_fname LIKE ‘Al%’

ORDER BY au_lname

10.Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. Figure 16-5 shows the appearance. On this occasion, only one name is returned that satisfies both criteria.

11.You can combine the LIKE keyword with the NOT keyword. For example, to select authors whose surname does not begin with B, you can use a WHERE clause like this:

WHERE au_lname NOT LIKE ‘B%’

Type the following code into the query pane of the Query Analyzer:

USE pubs

SELECT au_lname, au_fname FROM dbo.authors

WHERE au_lname NOT LIKE ‘B%’

ORDER BY au_lname

Figure 16-4

370

Regular Expression Functionality in SQL Server 2000

Figure 16-5

12.Press F5 or click the blue right-pointing arrow above the query pane to run the Transact-SQL code. When the code is run, all authors in the pubs database are returned except for those whose surname begins with B.

How It Works

The pattern B% matches the character B or b when it occurs at the beginning of a value and is then followed by any number of any characters. Occurrences of the character b, either case, that occur after the beginning of the word are not matched.

The pattern %b% matches the character B or b anywhere in a last name. The % at the beginning of %b% matches zero or more characters. When it matches zero characters, it can match surnames beginning with B.

The pattern %nt% combines a character sequence nt with a preceding % metacharacter and a following % metacharacter. Any surname containing the character sequence nt is matched.

371