ToyboxCreations SELECT * FROM [My Life]

20Aug/054

SQL 101 – The Advanced LIKE Clauses

As I promised, this is my post on advanced LIKE clauses.  Previously I've only shown you how to do a wildcard search that would act like the dos command dir A*.*, returning all the files that start with the letter A.  But there is far more you can do with the LIKE operator.

Wildcards themselves are actually characters that have special meanings within SQL.  Wildcard searching can be used only with VARCHAR fields; you can't use wildcards to search fields of nontext datatypes.  Full TEXT fields support an additional library of methods for searching for matches inside those fields, so let's leave that for next time.  For now, everything will work in a VARCHAR or NVARCHAR field.

The Percent Sign % Wildcard

The most frequently used wildcard is the percent sign %.  This is the wildcard I first introduced you to in my previous post.  % means match any number of occurrences of any character.  Wildcards can be used anywhere within the search pattern, and multiple wildcards can be used as well. The following example uses two wildcards, one at either end of the pattern:

SELECT
   productName
FROM Products
WHERE
   productName LIKE '%en%'

productName
-----------
pencil
pen

The Underscore _ Wildcard

Another useful wildcard is the underscore _. The underscore is used just like %, but instead of matching multiple characters, the underscore matches just a single character.

SELECT
   productName
FROM Products
WHERE
   productName LIKE '_en'

productName
-----------
pen

The Brackets [] Wildcard

The brackets [] wildcard is used to specify a set of characters, any one of which must match a character in the specified position.  This is where you can really get into some powerful comparisons.  But for this example, we're just going to use it to show those products that begin with p, have a character between b and f for the second letter, and then have anything after that...like I said, easy.

SELECT
   productName
FROM Products
WHERE
   productName LIKE 'p[b-f]%'

productName
-----------
pencil
pen

Negating a Range

If you add ^ to a range, it checks for all characters NOT in that range.  If we add it to our last example, the results change dramatically.

SELECT
   productName
FROM Products
WHERE
   productName LIKE 'p[^b-f]%'

productName
-----------
paper

Conclusion

I'm not telling you to use wildcards all the time, but they have their uses.  Be carefule where you place them, you could could dramatically different results.  Remember to test your code with several scenarios or against several data sets before releasing your code into production.  If you keep this in mind, this technique can become a powerful tool in your SQL toolbelt.

If you have any questions, send them in!  I'm here to help!

Previous: SELECT, Filtering Results (Part 2) Next: Calculated Fields

Related posts:

  1. SQL 101 – String Manipulation Functions
  2. SQL 101 – SELECT, Filtering Results
  3. SQL 101 – Combining Query Results
  4. SQL 101 – SELECT, Filtering Results (Part 2)
  5. SQL 101 – SELECT

About Shannon Lowder

Shannon Lowder is the Database Engineer you've been looking for! Look no further for expertise in: Business Analysis to gather the business requirements for the database; Database Architecting to design the logical design of the database; Database Development to actually build the objects needed by the business logic; finally, Database Administration to keep the database running in top form, and making sure there is a disaster recovery plan.
Comments (4) Trackbacks (0)
  1. Really helpfull..

  2. It is very useful for beginner to learnl basics about Database and Relational Database.


Leave a comment


No trackbacks yet.