LIKE Operator
Description
The LIKE operator is used in SQL queries to match strings based on a specified pattern. It helps you find data that contains specific characters or patterns. When using the LIKE operator, you can use wildcards to represent one or more characters.
Parameters
str(string): The original string to be matched.pattern(string): The pattern string containing wildcards.escape_char(optional): A single character used to escape the wildcard characters%and_in the pattern, treating them as literal characters.
Return Value
Returns a boolean value indicating whether str matches pattern.
Wildcard Description
%: Represents any number of characters (including zero characters)._: Represents any single character.- The default escape character is
\. A custom escape character can be specified using theESCAPEclause.
Syntax
Examples
- Match strings starting with "Hello":
Result:
- Match strings containing "lo":
Result:
- Match strings that contain "lo" and end with "ld":
Result:
- Use a single-character wildcard to match strings containing "oW":
Result:
- Use the NOT keyword to find strings that do not match a specific pattern:
Result:
- Use the ESCAPE clause to match strings containing a literal
%:
Result:
- Combine the ESCAPE clause with wildcards:
Result:
- Use ESCAPE to match strings containing a literal
_:
Result:
Notes
- The ESCAPE character must be a single constant character.
- The ESCAPE character can only be followed by
%,_, or the ESCAPE character itself. Following it with any other character results in an error. - ILIKE (case-insensitive LIKE) also supports the ESCAPE clause.
