LIKE Conditions

Use LIKE conditions in subqueries to use wildcards in the WHERE clause to perform pattern matching.

The syntax for LIKE conditions is:

expressionNOT ] LIKE patternESCAPE escape-expr ]

The LIKE condition can evaluate as TRUE, FALSE, or UNKNOWN. You can use LIKE only on string data.

You cannot use subqueries inside a LIKE predicate.

LIKE predicates that start with characters other than wildcard characters may execute faster if an HG or LF index is available.

Certain LIKE predicates execute faster, if a WD index is available.

Without the NOT keyword, the condition evaluates as TRUE if expression matches the pattern. If either expression or pattern is the NULL value, this condition is UNKNOWN. The NOT keyword reverses the meaning of the condition but leaves UNKNOWN unchanged.

The pattern might contain any number of wildcard characters. The wildcard characters are:

Wildcard

Matches

_ (underscore)

Any one character

% (percent)

Any string of zero or more characters

[]

Any single character in the specified range or set

[^]

Any single character not in the specified range or set

All other characters must match exactly.

For example, the search condition:

name LIKE 'a%b_'

is TRUE for any row where name starts with the letter a and has the letter b as its second-to-last character.

If you specify an escape-expr, it must evaluate to a single character. The character can precede a percent, an underscore, a left square bracket, or another escape character in the pattern to prevent the special character from having its special meaning. When escaped in this manner, a percent matches a percent, and an underscore matches an underscore.

Patterns Supported

All patterns of 126 characters or less are supported.

Some patterns between 127 and 254 characters are supported, but only under certain circumstances. See the following subsections for examples.

All patterns 255 characters or greater are not supported.

Patterns Between 127 and 254 Characters: Example 1

Under specific circumstances where adjacent constant characters exist in your pattern, patterns of length between 127 and 254 characters are supported. Each constant character in the string pattern requires two bytes, even if the character is a single-byte character. The string pattern in the LIKE predicate must be less than 256 bytes (or 255/2 characters) or else the following error appears:
There was an error reading the results of the SQL statement.
The displayed results may be incorrect or incomplete.
Cannot compile Like pattern: either bad pattern or pattern too long.

SAP Sybase IQ collapses adjacent constant characters into a single character. For example, consider the following LIKE predicate with a string length of 130 characters:

select col2 from tablen where col2 like '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456%%%%' ;

SAP Sybase IQ collapses the four adjacent constant characters %%%% at the end of the string into one % character, thereby reducing the length of the string from 130 characters to 127. This is less than the maximum of 256 bytes (or 255/2 characters), and no error is generated.

Therefore, if your LIKE predicate contains adjacent constants in the string, patterns of length between 127 and 254 characters are supported as long as the total length of the collapsed string is less than 256 bytes (or 255/2 characters).

Patterns between 127 and 254 characters: example 2

In this example, the constant characters 7890 replace the four adjacent constant characters %%%% at the end of the 130-character LIKE predicate:
select col2 from tablen where col2 like '1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890' ;

In this case, no characters are collapsed. The character string length remains at 130 characters and SAP Sybase IQ generates an error.

Patterns Between 127 and 254 Characters: Example 3

In this example, four adjacent underscores ____ (special characters) replace the four constant characters %%%% at the end of the 130-character LIKE predicate:
select col2 from tablen where col2 like '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456____' ;

SAP Sybase IQ does not collapse adjacent special characters. The string length remains at 130 characters and SAP Sybase IQ generates an error.

Patterns Between 127 and 254 Characters: Example 4

In this example, the range [1-3] replaces the four constant characters %%%% at the end of the 130-character LIKE predicate:
select col2 from tablen where col2 like '123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456[1-3]' ;

The length of the LIKE predicate in bytes is calculated as follows: 126 (for the constant characters) * 2 + 1 (for the 1 in brackets) + 1 ( for the 3 in brackets) + 2 ( for the Set state and Range state expression).

This equals 256 bytes, and therefore SAP Sybase IQ generates an error.

Searching for One of a Set of Characters

You can specify a set of characters to look for by listing the characters inside square brackets. For example, the following condition finds the strings smith and smyth:

LIKE 'sm[iy]th'

Searching for One of a Range of Characters

Specify a range of characters to look for by listing the ends of the range inside square brackets, separated by a hyphen. For example, the following condition finds the strings bough and rough, but not tough:

LIKE '[a-r]ough'

The range of characters [a-z] is interpreted as “greater than or equal to a, and less than or equal to z,” where the greater than and less than operations are carried out within the collation of the database. For information on ordering of characters within a collation, see How the Collation Sequence Sorts Characters in Administration: Globalization.

The lower end of the range must precede the higher end of the range. For example, a LIKE condition containing the expression [z-a] returns no rows, because no character matches the [z-a] range.

Unless the database is created as case-sensitive, the range of characters is case-insensitive. For example, the following condition finds the strings Bough, rough, and TOUGH:

LIKE '[a-z]ough'

If the database is created as a case-sensitive database, the search condition is case-sensitive also.

Combining Searches for Ranges and Sets

You can combine ranges and sets within square brackets. For example, the following condition finds the strings bough, rough, and tough:

LIKE '[a-rt]ough'

The bracket [a-mpqs-z] is interpreted as “exactly one character that is either in the range a to m inclusive, or is p, or is q, or is in the range s to z inclusive.”

Searching for One Character Not in a Range

Use the caret character (^) to specify a range of characters that is excluded from a search. For example, the following condition finds the string tough, but not the strings rough, or bough:

LIKE '[^a-r]ough'

The caret negates the entire contents of the brackets. For example, the bracket [^a-mpqs-z] is interpreted as “exactly one character that is not in the range a to m inclusive, is not p, is not q, and is not in the range s to z inclusive.”

Special Cases of Ranges and Sets

Any single character in square brackets indicates that character. For example, [a] matches just the character a. [^] matches just the caret character, [%] matches only the percent character (the percent character does not act as a wildcard character in this context), and [_] matches just the underscore character. Also, [[] matches only the character [.

Other special cases are:

  • The expression [a-] matches either of the characters a or -.

  • The expression [] is never matched and always returns no rows.

  • The expressions [ or [abp-q are ill-formed expressions, and give syntax errors.

  • You cannot use wildcard characters inside square brackets. The expression [a%b] finds one of a%, or b.

  • You cannot use the caret character to negate ranges except as the first character in the bracket. The expression [a^b] finds one of a, ^, or b.

Compatibility

The ESCAPE clause is supported by SAP Sybase IQ only.

Note: For information on support of the LIKE predicate with large object data and variables, see Unstructured Data Queries in Unstructured Data Analytics.

Users must be specifically licensed to use the large object data types LONG BINARY and LONG VARCHAR. For details on the Unstructured Data Analytics Option, see Unstructured Data Analytics.

Related reference
PATINDEX Function [String]
LOCATE Function [String]