SQL Like and Wildcard Operators - Wild Card operators in SQL



Demo wikitechydatabase

  • Below is a selection from the wikitechytable table used in the examples:
sql-like-operator-1

The SQL LIKE Operator

  • The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

SQL Wildcard Characters

  • A wildcard character is used to substitute one or more characters in a string.
  • Wildcard characters are used with the LIKE operator.
  • The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

Wildcard Characters

Symbol Description
% Represents zero or more characters
_ Represents a single character
[] Represents any single character within the brackets
- Represents any single character within the specified range

Starts With

  • To return records that starts with a specific letter or phrase, add the % at the end of the letter or phrase.
  • Return all wikitechytable that starts with 'p':
SELECT  * FROM wikitechytable WHERE name LIKE 'p%';

Output

sql-starts-with-2

Ends With

  • To return records that ends with a specific letter or phrase, add the % at the beginning of the letter or phrase.
  • Return all wikitechytable that ends with 't':
SELECT  * FROM wikitechytable WHERE course LIKE '%t';

Output

sql-ends-with-3

Contains

  • To return records that contains a specific letter or phrase, add the % both before and after the letter or phrase.
  • Return all wikitechytable that contains the phrase 'm'
SELECT  * FROM wikitechytable WHERE gender LIKE '%m%';

Output

sql-contains-4

Using the _ Wildcard

  • The _ wildcard represents a single character.
  • It can be any character or number, but each _ represents one, and only one, character.
  • Return all wikitechytable with a gender starting with any character, followed by "ale":
select * from wikitechytable WHERE gender LIKE '_ale';

Output

sql-wildcard-5
  • Return all wikitechytable with a gender starting with "fe", followed by any 2 characters, ending with "le":
select * from wikitechytable WHERE gender LIKE 'fe__le';
sql-wildcard-6

Using the [] Wildcard

  • The [] wildcard returns a result if any of the characters inside gets a match.
  • Return all wikitechytable starting with either "k" or "i":
select * from wikitechytable WHERE name LIKE '[ki]%';

Output

sql-wildcard-7

Using the - Wildcard

  • The - wildcard allows you to specify a range of characters inside the [] wildcard.
  • Return all wikitechytable starting with "k", "l", "m", "n", "o" or "p":
select * from wikitechytable where name LIKE '[k-p]%'; 

Output

sql-wildcard-8

Combine Wildcards

  • Any wildcard, like % and _ , can be used in combination with other wildcards.
  • Return all wikitechytable that starts with "s" and are at least 2 characters in length:
select * from wikitechytable where course LIKE 's__%'; 

Output

sql-wildcard-9
  • Return all wikitechytable that have "q" in the second position:
select * from wikitechytable where course LIKE '_q%'; 

Output

sql-wildcard-10

Related Searches to SQL Like and Wildcard Operators - Wild Card operators in SQL