|SQL LIKE Keyword by SQL Tutorial|
SQL LIKE Keyword
- %(percentage sign): % sign means only a character, multiple characters or no characters.
- _(underscore sign): _ sign means exactly one character is allowed. It does not allow zero or more than one characters.
SQL LIKE Syntax
where columnN like pattern;
SQL LIKE Bonus Tips
- Both % and _ can be used together to create string patterns.
- We can also use the AND and OR operators to create complicate where condition where it satisfies more than one patterns.
- MS Access uses a ?(question mark) instead of _(underscore).
Common pattern examples for LIKE operator
- ‘a%’: String starts with “a”
- ‘%a’: String ends with “a”
- ‘%xyz%’: String having “xyz” in it. (start, end or anywhere in the middle)
- ‘_b%’: String having “b” in the 2nd position
- ‘a__’: String that starts with “a” and 3 character length.
- ‘a_%_%’: String starts with “a” and at least of 3 characters in length.
- ‘a%z’: String which starts with “a” and ends with “z”.
- ‘%b_’: String which has character “b” as 2nd last character in it.
SQL LIKE Usage Examples
“LIKE” Example #6: Find all the authors whose name starts with a and should contain 3 or more characters.
“LIKE” Example #7: Find all the authors whose names start with “a” and ends with “z”.
“LIKE” Example #8: Find all the authors who have b as 2nd last characters in their names.
What we learned in this SQL LIKE topic?
We learned SQL LIKE keyword and its operands. It takes string patterns as one of the operand which uses % or _, or both while comparing string data.