Technical FAQs

Ask a Question

What are SQL Wildcards?

Issue
Limiting the resultant set in a query statement.

Product Line
SQL Server 2012

Environment
SQL Statements

Cause
Making the 'LIKE' operator more flexible.

Resolution
*Warning: Irreparable database damage can occur. This procedure should only be performed by users with good understanding of writing SQL queries. Databases should be backed up prior to performing this procedure.*

Four wildcards can be used in a sql query:

1.  %
Descrition: Any string of zero or more characters.
Example: WHERE title LIKE '%computer%' finds all book titles with the word 'computer' anywhere in the book title.

2.  _ (underscore)
Descrition: Any single character.
Example: WHERE name LIKE '_ean' finds all four-letter names that end with ean (Dean, Sean, and so on).

3.  []
Description: Any single character within the specified range ([a-f]) or set ([abcdef]).
Example: WHERE name LIKE '[C-P]arsen' finds names ending with 'arsen' and starting with any single character between C and P, for example Carsen, Larsen, Karsen, and so on.

4.  [^]
Description: Any single character not within the specified range ([^a-f]) or set ([^abcdef]).
Example: WHERE name LIKE 'de[^b]%' finds all names starting with de and where the following letter is not b.
 
Was this helpful?
What can we do to improve the information ?