SQL LIKE Operator and Wildcard Characters
LIKE operator in SQL is used with WHERE Clause to select a specific row or value which matches the specified pattern.
We use the following wildcards with LIKE operator
Symbol | Description |
---|---|
% | Represents zero or more character |
_ | Represents a single character |
[] | Represents any single character within bracket |
^ | Represents any character not in bracket |
- | Represents a single character within a specific range |
Syntax:
SELECT col(s) FROM table_name WHERE col_name LIKE pattern;
Example:
SELECT * FROM Employee WHERE emp_fname LIKE "[Pp]%
Let say you want to find all rows:
- whose column col_name starts with P :
SELECT * FROM table_name WHERE col_name LIKE "P%"
- which ends with P
SELECT * FROM table_name WHERE col_name LIKE "%P"
- starts with P, ends with t and contain exactly 4 characters:
SELECT * FROM table_name WHERE col_name LIKE "P_ _ t"
Comments
Post a Comment