The LIKE operator in SQL is used in a WHERE clause to search for a specified pattern in a column. It is typically used with wildcard characters to filter records based on pattern matching.
Wildcard Characters in SQL:
%: Represents zero, one, or multiple characters._: Represents a single character.
Syntax
Examples
1. Match Strings Starting with a Specific Character
This query finds all names that start with "A."
2. Match Strings Ending with a Specific Character
This query finds all names that end with "n."
3. Match Strings Containing a Specific Sequence
This query finds all names containing "an."
4. Match Strings of a Specific Length
This query finds all names that are exactly three characters long.
5. Case Sensitivity
- In most databases, the
LIKEoperator is case-insensitive (e.g., MySQL). - To perform a case-sensitive match in databases like MySQL, use the
BINARYkeyword:
Advanced Patterns
Escape Characters
To search for special characters like % or _, use an escape character:
This query finds job titles that include "50%".
Combining with Other Conditions
This query finds names starting with "A" where the age is greater than 30.
No comments:
Post a Comment