LIKE

Log Parser

LIKE

<field_expr> [ NOT ] LIKE <like_mask>

Determines whether or not a given character string matches a specified pattern. A pattern can include regular characters and wildcard characters. During pattern matching, regular characters must yield a case-insensitive match with the characters specified in the character string. Wildcard characters, however, can be matched with arbitrary fragments of the character string. Using wildcard characters makes the LIKE operator more flexible than using the = and != string comparison operators.

The wildcard characters that can be used in a LIKE pattern are:

  • _ (underscore character): matches any single character
    Examples:
    LIKE 'ab_d': matches all the four-letter strings that start with "ab" and end with "d" (e.g. "abcd", "AB+d")
    LIKE 'a_c_': matches all the four-letter strings that have "a" in the first position and "c" in the third position (e.g. "abcd", "Akck")
  • % (percent character): matches any string of zero or more characters
    Examples:
    LIKE '%.asp' matches all the strings ending with ".asp" (e.g. "/default.asp", ".ASP")
    LIKE '%error%' matches all the strings containing "error" (e.g. "an error has been found", "ERROR")


Remarks:

  • Similarly to STRING constants, characters in a LIKE pattern can be escaped with the '\' (backslash) character or encoded with the \uxxxx notation.
  • Wildcard pattern matching characters can be used as literal characters. To use a wildcard character as a literal character, escape the wildcard character with the '\' (backslash) character.
    Examples:
    LIKE 'ab\_d': matches the "ab_d" string (e.g. "ab_d", "AB_d")
    LIKE 'a\%c%': matches all the strings that start with "a%c" (e.g. "a%cdefg", "A%c")
  • When executing a Log Parser query from within a command-line batch file, using the % wildcard character might yeld unexpected results.
    For example, consider the following batch file:
    @echo off
    LogParser "SELECT * FROM SYSTEM WHERE Message LIKE '%ERROR%'"
    
    When this batch file is executed, the command-line shell interpreter will assume that "%ERROR%" is a reference to an environment variable, and it will try to replace this string with the value of the environment variable. In most cases, such an environment variable will not exist, and the actual command executed by the shell will look like:
    LogParser "SELECT * FROM SYSTEM WHERE Message LIKE ''"
    
    Which would yeld the following error:
    Error: Syntax Error: <term2>: no valid LIKE mask
    
    To avoid this problem, use double %% wildcard characters when writing a command-line batch file, as in the following example:
    @echo off
    LogParser "SELECT * FROM SYSTEM WHERE Message LIKE '%%ERROR%%'"
    


Examples

A. LIKE

The following example WHERE clause finds all the URL's in an IISW3C log file that end with ".htm":
WHERE cs-uri-stem LIKE '%.htm'

B. NOT LIKE

The following example WHERE clause finds all the Event Log messages that do not contain "error":
WHERE Message NOT LIKE '%error%'


See also:

Expressions
Field-Expressions


© 2004 Microsoft Corporation. All rights reserved.