An expression that specifies the string that is to be matched.
The
expression can be specified in the same way as
match-expression with
the following restrictions:
- No element in the expression can be of type
CLOB or DBCLOB. In addition it cannot be a BLOB file reference variable.
- The actual length of pattern-expression cannot
be more than 32 672 bytes.
A
simple
description of the use of the LIKE predicate is that the pattern
is used to specify the conformance criteria for values in the
match-expression,
where:
- The underscore character (_) represents any single character.
- The percent sign (%) represents a string of zero or more characters.
- Any other character represents itself.
If the pattern-expression needs
to include either the underscore or the percent character, the escape-expression is
used to specify a character to precede either the underscore or the
percent character in the pattern.
A
rigorous description of
the use of the LIKE predicate follows. Note that this description
ignores the use of the
escape-expression;
its use is covered later.
- Let m denote the value of match-expression and
let p denote the value of pattern-expression.
The string p is interpreted as a sequence
of the minimum number of substring specifiers so each character of p is
part of exactly one substring specifier. A substring specifier is
an underscore, a percent sign, or any non-empty sequence of characters
other than an underscore or a percent sign.
The result of the
predicate is unknown if
m or
p is
the null value. Otherwise, the result is either true or false. The
result is true if
m and
p are
both empty strings or there exists a partitioning of
m into
substrings such that:
- A substring of m is a sequence of zero
or more contiguous characters and each character of m is
part of exactly one substring.
- If the nth substring specifier is an
underscore, the nth substring of m is
any single character.
- If the nth substring specifier is a
percent sign, the nth substring of m is
any sequence of zero or more characters.
- If the nth substring specifier is neither
an underscore nor a percent sign, the nth
substring of m is equal to that substring
specifier and has the same length as that substring specifier.
- The number of substrings of m is the
same as the number of substring specifiers.
Thus, if p is an empty string
and m is not an empty string, the result
is false. Similarly, it follows that if m is
an empty string and p is not an empty string
(except for a string containing only percent signs), the result is
false.
The predicate m NOT LIKE p is
equivalent to the search condition NOT (m LIKE p).
When the escape-expression is
specified, the pattern-expression must not
contain the escape character identified by the escape-expression,
except when immediately followed by the escape character, the underscore
character, or the percent sign character (SQLSTATE 22025).
If
the
match-expression is a character string
in an MBCS database, it can contain mixed data. In this case, the
pattern can include both SBCS and non-SBCS characters. For non-Unicode
databases, the special characters in the pattern are interpreted as
follows:
- An SBCS halfwidth underscore refers to one SBCS character.
- A non-SBCS fullwidth underscore refers to one non-SBCS character.
- An SBCS halfwidth or non-SBCS fullwidth percent sign refers to
zero or more SBCS or non-SBCS characters.
In a Unicode database, there is really no distinction
between "single-byte" and "non-single-byte" characters. Although the
UTF-8 format is a "mixed-byte" encoding of Unicode characters, there
is no real distinction between SBCS and non-SBCS characters in UTF-8.
Every character is a Unicode character, regardless of the number of
bytes in UTF-8 format.
In a Unicode graphic column, every
non-supplementary character, including the halfwidth underscore character
(U&'\005F') and the halfwidth percent sign character (U&'\0025'),
is two bytes in width. In a Unicode database, special characters in
a pattern are interpreted as follows:
- For character strings, a halfwidth underscore character (X'5F')
or a fullwidth underscore character (X'EFBCBF') refers to one Unicode
character, and a halfwidth percent sign character (X'25') or a fullwidth
percent sign character (X'EFBC85') refers to zero or more Unicode
characters.
- For graphic strings, a halfwidth underscore character (U&'\005F')
or a fullwidth underscore character (U&'\FF3F') refers to one
Unicode character, and a halfwidth percent sign character (U&'\0025')
or a fullwidth percent sign character (U&'\FF05') refers to zero
or more Unicode characters.
- To be recognized as special characters when a locale-sensitive
UCA-based collation is in effect, the underscore character and the
percent sign character must not be followed by non-spacing combining
marks (diacritics). For example, the pattern U&'%\0300' (percent
sign character followed by non-spacing combining grave accent) will
be interpreted as a search for and not as a search for zero or more Unicode
characters followed by a letter with a grave accent.
A Unicode supplementary character is stored
as two graphic code points in a Unicode graphic column. To match a
Unicode supplementary character in a Unicode graphic column, use one
underscore if the database uses locale-sensitive UCA-based collation,
and two underscores otherwise. To match a Unicode supplementary character
in a Unicode character column, use one underscore for all collations.
To match a base character with one or more trailing non-spacing combining
characters, use one underscore if the database uses locale-sensitive
UCA-based collation. Otherwise, use as many underscore characters
as the number of non-spacing combining characters plus the base character.
This optional argument is an expression that specifies a character
to be used to modify the special meaning of the underscore (_) and
percent (%) characters in the pattern-expression.
This allows the LIKE predicate to be used to match values that contain
the actual percent and underscore characters. The expression can
be specified by any one of:
- A constant
- A special register
- A global variable
- A host variable
- A scalar function whose operands are any of the above
- An expression concatenating any of the above
with the restrictions that:
- No element in the expression can be of type
CLOB or DBCLOB. In addition, it cannot be a BLOB file reference variable.
- For character columns, the result of the expression must be one
character, or a binary string containing exactly one byte (SQLSTATE
22019).
- For graphic columns, the result of the expression must be one
character (SQLSTATE 22019).
- The result of the expression must not be a non-spacing combining
character sequence (for example, U&'\0301', Combining Acute Accent).
When escape characters are present in the pattern string,
an underscore, percent sign, or escape character can represent a literal
occurrence of itself. This is true if the character in question is
preceded by an odd number of successive escape characters. It is
not true otherwise.
In a pattern, a sequence of successive
escape characters is treated as follows:
- Let S be such a sequence, and suppose that S is not part of a
larger sequence of successive escape characters. Suppose also that
S contains a total of n characters. Then the rules governing S depend
on the value of n:
- If n is odd, S must be followed by an underscore or percent sign
(SQLSTATE 22025). S and the character that follows it represent (n-1)/2
literal occurrences of the escape character followed by a literal
occurrence of the underscore or percent sign.
- If n is even, S represents n/2 literal occurrences of the escape
character. Unlike the case where n is odd, S could end the pattern.
If it does not end the pattern, it can be followed by any character
(except, of course, an escape character, which would violate the assumption
that S is not part of a larger sequence of successive escape characters).
If S is followed by an underscore or percent sign, that character
has its special meaning.
Following is an illustration of the effect of successive
occurrences of the escape character which, in this case, is the back
slash (\).
- Pattern string
- Actual Pattern
- \%
- A percent sign
- \\%
- A back slash followed by zero or more arbitrary characters
- \\\%
- A back slash followed by a percent sign