SQL WHERE LIKE SELECT WHERE LIKE Wildcard OR Examples

SQL WHERE LIKE SELECT WHERE LIKE Wildcard OR Examples

The SQL Server LIKE is a logical operator that determines if a character string matches a specified pattern. 
The LIKE operator is used in the WHERE clause of the SELECT, UPDATE, and DELETE statements to filter rows based on pattern matching. 
A pattern may include regular characters and wildcard characters.

WHERE LIKE determines if a character string matches a pattern.
WHERE LIKE supports two wildcard match options: % and _.

Syntax

SELECT column_names
  FROM table_name
 WHERE column_name LIKE value

_ - The underscore represents a single character
% - The percent sign represents zero, one, or multiple characters

SQL statement selects all customer with a Name that does NOT start with "a"

SELECT * FROM Customer
WHERE Name NOT LIKE 'a%'

SQL statement selects all customer with a Name that starts with "a" and ends with "o"

SELECT * FROM Customer
WHERE Name LIKE 'a%o'

SQL statement selects all customer with a Name that starts with "a" and are at least 3 characters in length

SELECT * FROM Customer
WHERE Name LIKE 'a__%'

SQL statement selects all customer with a Name that have "r" in the second position

SELECT * FROM Customer
WHERE Name LIKE '_r%'

SQL statement selects all customer with a Name that have "or" in any position

SELECT * FROM Customer
WHERE Name LIKE '%or%'

SQL statement selects all customer with a Name starting with "a"

SELECT * FROM Customer
WHERE Name LIKE 'a%'

SQL statement selects all customer with a Name ending with "a"

SELECT * FROM Customer
WHERE Name LIKE '%a'

 

2020-04-22