SQL Like Opt.
SQL Like Opt.
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o
sqlCopy code
SELECT column1, column2, ... FROM table_name WHERE column_name LIKE pattern ;
%: Represents zero, one, or multiple characters. For example, '%apple%' would match any string that contains "apple" as a
substring, regardless of what comes before or after it.
_: Represents a single character. For example, 'h_t' would match "hat," "hot," or any other three-letter word with "h" as the
first character, "t" as the last character, and any character in between.
It's important to note that using the LIKE operator with a leading wildcard (e.g., '%something') can be inefficient in large
databases, as it may require a full table scan to find the matching rows. If possible, it's best to avoid leading wildcards for better
performance.
Additionally, some database systems provide more advanced pattern matching capabilities using regular expressions (e.g.,
REGEXP or RLIKE), which allow for more complex pattern matching scenarios.