>>-match-expression--+-----+--LIKE--pattern-expression--+---------------------------+->< '-NOT-' '-ESCAPE--escape-expression-'
The LIKE predicate searches for strings that have a certain pattern. The pattern is specified by a string in which the underscore and percent sign have special meanings. Trailing blanks in a pattern are a part of the pattern.
If the value of any of the arguments is null, the result of the LIKE predicate is unknown.
The match-expression, pattern-expression, and escape-expression must identify strings or numbers. A numeric argument is cast to a character string before evaluating the predicate. For more information on converting numeric to a character string, see VARCHAR. The values for match-expression, pattern-expression, and escape-expression must either all be binary strings or none can be binary strings. The three arguments can include a mixture of character strings and graphic strings.
None of the expressions can yield a distinct type. However, it can be a function that casts a distinct type to its source type.
If the operands of the predicate are SBCS data, mixed data, or Unicode data, and if the sort sequence in effect at the time the statement is executed is not *HEX, then the comparison of the operands is performed using weighted values for the operands. The weighted values are based on the sort sequence. An ICU sort sequence is not allowed with a LIKE predicate.
With character strings, the terms character, percent sign, and underscore in the following discussion refer to single-byte characters. With graphic strings, the terms refer to double-byte or Unicode characters. With binary strings, the terms refer to the code points of those single-byte characters.
Simple description: A simple description of the LIKE pattern is as follows:
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 percent character in the pattern.
Rigorous description: Let x denote a value of match-expression and y denote the value of pattern-expression.
The string y is interpreted as a sequence of the minimum number of substring specifiers so each character of y is part of exactly one substring specifier. A substring specifier is an underscore, a percent sign, or any nonempty sequence of characters other than an underscore or a percent sign.
The result of the predicate is unknown if x or y is the null value. Otherwise, the result is either true or false. The result is true if x and y are both empty strings or if there exists a partitioning of x into substrings such that:
It follows that if y is an empty string and x is not an empty string, the result is false. Similarly, it follows that if y is an empty string and x is not an empty string consisting of other than percent signs, the result is false.
The predicate x NOT LIKE y is equivalent to the search condition NOT(x LIKE y).
If necessary, the CCSID of the match-expression, pattern-expression, and escape-expression are converted to the compatible CCSID between the match-expression and pattern-expression.
Mixed data: If the column is mixed data, the pattern can include both SBCS and DBCS characters. The special characters in the pattern are interpreted as follows:
Unicode data: For Unicode, the special characters in the pattern are interpreted as follows:
When the LIKE predicate is used with Unicode data, the Unicode percent sign and underscore use the code points indicated in the following table:
Character | UTF-8 | UTF-16 or UCS-2 |
---|---|---|
Half-width % | X'25' | X'0025' |
Full-width % | X'EFBC85' | X'FF05' |
Half-width _ | X'5F' | X'005F' |
Full-width _ | X'EFBCBF' | X'FF3F' |
The full-width or half-width % matches zero or more characters. The full-width or half width _ character matches exactly one character. (For EBCDIC data, a full-width _ character matches one DBCS character.)
Parameter marker:
When the pattern specified in a LIKE predicate is a parameter marker, and a fixed-length character variable is used to replace the parameter marker; specify a value for the variable that is the correct length. If a correct length is not specified, the select will not return the intended results.
For example, if the variable is defined as CHAR(10), and the value WYSE% is assigned to that variable, the variable is padded with blanks on assignment. The pattern used is
'WYSE% '
This pattern requests the database manager to search for all values that start with WYSE and end with five blank spaces. If you intended to search for only the values that start with 'WYSE' you should assign the value 'WYSE%%%%%%' to the variable.
For example, if '+' is the escape character, any occurrences of '+' other than '++', '+_', or '+%' in the pattern-expression is an error.
The following example shows the effect of successive occurrences of the escape character, which in this case is the plus sign (+).
When the pattern string is... | The actual pattern is... |
---|---|
+% | A percent sign |
++% | A plus sign followed by zero or more arbitrary characters |
+++% | A plus sign followed by a percent sign |
Search for the string 'SYSTEMS' appearing anywhere within the PROJNAME column in the PROJECT table.
SELECT PROJNAME FROM PROJECT WHERE PROJECT.PROJNAME LIKE '%SYSTEMS%'
Search for a string with a first character of 'J' that is exactly two characters long in the FIRSTNME column of the EMPLOYEE table.
SELECT FIRSTNME FROM EMPLOYEE WHERE EMPLOYEE.FIRSTNME LIKE 'J_'
In this example:
SELECT * FROM TABLEY WHERE C1 LIKE 'AAAA+%BBB%' ESCAPE '+'
'+' is the escape character and indicates that the search is for a string that starts with 'AAAA%BBB'. The '+%' is interpreted as a single occurrence of '%' in the pattern.
Assume that a distinct type named ZIP_TYPE with a source data type of CHAR(5) exists and an ADDRZIP column with data type ZIP_TYPE exists in some table TABLEY. The following statement selects the row if the zip code (ADDRZIP) begins with '9555'.
SELECT * FROM TABLEY WHERE CHAR(ADDRZIP) LIKE '9555%'
The RESUME column in sample table EMP_RESUME is defined as a CLOB. If the variable LASTNAME has a value of 'JONES', the following statement selects the RESUME column when the string JONES appears anywhere in the column.
SELECT RESUME FROM EMP_RESUME WHERE RESUME LIKE '%'||LASTNAME||'%'
In the following table of EBCDIC examples, assume COL1 is mixed data. The table shows the results when the predicates in the first column are evaluated using the COL1 values from the second column:
(C) Copyright IBM Corporation 1992, 2006. All Rights Reserved.