sql_like
SQL LIKE Operator: Pattern Matching in Strings
Section titled “SQL LIKE Operator: Pattern Matching in Strings”The LIKE operator is used in a WHERE clause to search for a specified pattern in a column containing text data. It’s a powerful tool for flexible string comparisons.
The SQL LIKE Operator Explained
Section titled “The SQL LIKE Operator Explained”When you need to find rows where a column’s string value matches a certain pattern rather than an exact string, the LIKE operator is your solution. It uses wildcard characters to define these patterns.
SQL LIKE Syntax
Section titled “SQL LIKE Syntax”SELECT column_name(s)FROM table_nameWHERE column_name LIKE pattern;Standard Wildcard Characters:
Section titled “Standard Wildcard Characters:”%(Percent sign): Represents zero, one, or multiple characters.'A%': Matches any string starting with ‘A’. (e.g., ‘Apple’, ‘Ant’, ‘A’)'%Z': Matches any string ending with ‘Z’. (e.g., ‘Buzz’, ‘Quiz’)'%word%': Matches any string containing ‘word’. (e.g., ‘keyword’, ‘password’)
_(Underscore): Represents a single character.'H_t': Matches ‘Hot’, ‘Hat’, ‘Hit’.'__X%': Matches any string where ‘X’ is the third character.
Case Sensitivity: The LIKE operator’s case sensitivity depends on the database system and the column’s collation settings. Some databases are case-sensitive by default (e.g., PostgreSQL), while others are not (e.g., SQL Server, MySQL on Windows). You can often use functions like LOWER() or UPPER() on both the column and the pattern for case-insensitive searches if needed: WHERE LOWER(column_name) LIKE LOWER(pattern);
Demonstration Database: Northwind
Section titled “Demonstration Database: Northwind”Let’s use the Northwind sample database.
A selection from the “Customers” table:
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22SwedenSQL LIKE Operator Examples
Section titled “SQL LIKE Operator Examples”- Select all customers with a City starting with the letter “L”:
SELECT CustomerName, CityFROM CustomersWHERE City LIKE 'L%';- Select all customers with a City ending with the letter “n”:
SELECT CustomerName, CityFROM CustomersWHERE City LIKE '%n';- Select all customers with a Country containing the pattern “land”:
SELECT CustomerName, CountryFROM CustomersWHERE Country LIKE '%land%';- Select all customers whose CustomerName has ‘o’ as the second letter:
SELECT CustomerNameFROM CustomersWHERE CustomerName LIKE '_o%';Using NOT LIKE
Section titled “Using NOT LIKE”The NOT LIKE operator allows you to select records that DO NOT match the pattern.
- Select all customers with a Country NOT containing the pattern “land”:
SELECT CustomerName, CountryFROM CustomersWHERE Country NOT LIKE '%land%';Escaping Wildcard Characters:
Section titled “Escaping Wildcard Characters:”If you need to search for a literal % or _ character, you must use the ESCAPE clause. For example, to find names containing ‘50%’:
SELECT ProductNameFROM ProductsWHERE ProductName LIKE '%50\%%' ESCAPE '\'; -- '\' is the escape character herePractical Application: LIKE is invaluable for search functionalities, data filtering based on partial information, and data validation rules.