12.1. Tricks with Patterns

The ‘_’ character tests much faster than the ‘%’ character. The reason is obvious: the parser that compares a string to the pattern needs only one operation to match an underscore before it can move to the next character, but has to do some look-ahead parsing to resolve a percentage sign. The wildcards can be inserted in the middle or beginning of a pattern. Thus, ‘B%K’ will match ‘BOOK’, ‘BLOCK’, and ‘BK’, but it will not match ‘BLOCKS’.

The parser would scan each letter and classify it as a wildcard match or an exact match. In the case of ‘BLOCKS’, the initial ‘B’ would be an exact match and the parser would continue; ‘L’, ‘O’, and ‘C’ have to be wildcard matches, since they don’t appear in the pattern string; ‘K’ ...

Get Joe Celko's SQL for Smarties, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.