LIKE wildcards

by Allan Svelmøe Hansen October 21, 2008 17:07

Today I want to mention the ”LIKE” wildcards.
Many people don’t seem to have realized/read that you can in fact use a regular expression type wildcard. Of course it is nowhere near regular expressions, but it is handy for various situations. The syntax can also be read in the msdn documentation (which incidently is where I fell over it some years ago).

Basically you have the standard wildcards; % and _
However you also have the range wildcard [] and [^] which works very similar to regular expressions.
This means that if I do a LIKE '[abc]%' it’ll match up against conditions where a word starts with a, b or c. This could also be written as '[a-c]%'.
To negate the effect you add a ^ character in the beginning of the string, like this '[^abc]%' which means any word which doesn’t begin with a, b or c.
These wildcards can also be used in for example the PATINDEX like.

This is very handy for many situations, if one remembers them.

Bookmark and Share DotnetKicks dotnetshoutout

Tags:

SQL

Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen | Modified by Mooglegiant

About:
Allan Svelmøe Hansen

My real name is Allan Svelmøe Hansen.
I live in Denmark, where I work as a developer for hedal:kruse:brohus using SQL Server and the .NET framework since 2004.
My primary fields of expertise is back-end data integration, database design and optimization.


       View Allan Svelmøe Hansen's profile on LinkedIn     

Disclaimer

The opinions expressed herein are my own personal opinions and thoughts and does not represent my employers view in any way, nor are my results guaranteed for all situations.
Content is presented "as is", with no warranty.