Showing posts with label wildcard. Show all posts
Showing posts with label wildcard. Show all posts

Wednesday, 20 July 2022

SQL LIKE Operator

The LIKE operator in SQL is used with the WHERE clause to get a result set that matches the given string pattern.

There are two wildcards often used with the LIKE operator,
  1. % (percent sign) - represents zero, one, or multiple characters
  2.  _ (underscore sign) - represents one, single character 

LIKE Operator Syntax

SELECT columnName,...
FROM tableName
WHERE columnName LIKE 'pattern';

Example of SQL LIKE operator & Wildcards


1. SQL Statement selects all Company with CompanyName that have value "Microsoft"

SELECT * FROM Company
WHERE CompanyName LIKE 'Microsoft';

2. SQL Statement selects all Company with CompanyName that have value "M" at the first position

SELECT * FROM Company
WHERE CompanyName LIKE 'M%';

3. SQL Statement selects all Company with CompanyName that have value "soft" at the end

SELECT * FROM Company
WHERE CompanyName LIKE '%soft';

4. SQL Statement selects all Company with CompanyName that have value "web" at any position

SELECT * FROM Company
WHERE CompanyName LIKE '%web%';

5. SQL Statement selects all Company with CompanyName that have value "o" at second position

SELECT * FROM Company
WHERE CompanyName LIKE '_o%';

6. SQL Statement selects all Company with CompanyName that have value "g" at first position and value "e" as last position

SELECT * FROM Company
WHERE CompanyName LIKE 'g%e';