AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL LIKE |
|
SQL > SQL Commands >
Like
The LIKE operator is used to filter the result set based on a string pattern. It is always used in the WHERE clause. The SQL LIKE operator searches for a pattern within a text column using wildcard characters:
% matches any sequence of characters and _ matches exactly one character.SyntaxThe syntax for the LIKE operator is as follows:
{PATTERN} often consists of wildcards. We saw several examples of wildcard matching in the previous section. ExampleWe use the following table for our example. Table Store_Information
We want to find all stores whose name contains 'AN'. To do so, we key in,
Result:
The "%" sign before 'AN' means that there may be 0, 1, or more characters before the pattern 'AN.' The "%" sign after 'AN' means that there may be 0, 1, or more characters after the pattern 'AN.' Out of the four store names, 'LOS ANGELES,' 'SAN DIEGO,' and 'SAN FRANCISCO' all contain this pattern. Frequently Asked QuestionsHow do I search for a literal percent sign or underscore with LIKE?Use an escape character. Most databases support the ESCAPE clause: e.g., How does LIKE differ from using = in a WHERE clause?The equality operator (=) requires an exact match of the entire value. LIKE allows partial or pattern-based matching with wildcards. Use = when you know the full value, and LIKE when you need to search for a substring or pattern. Can I use LIKE with numbers or dates?LIKE is designed for character string matching. While some databases will implicitly convert numbers or dates to strings for a LIKE comparison, it's best practice to use comparison operators (>=, <=, BETWEEN) for numeric and date ranges. What is SQL ILIKE and how is it different from LIKE?ILIKE is a PostgreSQL extension that performs case-insensitive pattern matching, equivalent to LIKE on a case-insensitive collation. In MySQL, you achieve the same effect by using a case-insensitive collation or the LOWER() function: ExercisesFor these exercises, assume we have a table called User_Sales with the following data: Table User_Sales
1) 1. Which of the following SQL statement is valid? (There can be more than one answer)
2. How many records will be returned by the following query? (Assuming the database is configured to be case-insensitive)
3. How many records will be returned by the following query? (Assuming the database is configured to be case-insensitive)
|
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.