Wednesday, November 2, 2011

SQL Server 2008 wildcards used in Query

I have just learn some useful wildcards for SQL Server 2008 recently.

( _ ) Underscore Wildcard character

Underscore wildcard character can be used to limit the search result for an exact number of characters.

Example: SELECT * FROM Employee WHERE LastName like 'Li_' will return all employees whose LastName like 'Lin' or 'Lim' but not 'Linux'.

(-) Wildcard Character

- wild card character can be used to limit the search results for character within a range.

Example: SELECT * FROM Employee WHERE LastName like '[A-E]%' will return all employees whose last name started with A to E.

(^-) Wildcard Character

^- wild card character can be used to limit the search results for character outside a range.

Example: SELECT * FROM Employee WHERE LastName like '[^A-E]%' will return all employees whose last name NOT started with A to E.