| Programming | Software Engineering | Web Design | Database | Operating Systems

Regular Expressions in T-SQL

Ken Henderson
Keywords: Regular Expressions,T-SQL
From: http://blogs.msdn.com/khen1234/comments/416392.aspx

Given that a recent blog of mine lamented the dangers of using xprocs, I thought it would be a good time to show some of the wonderful stuff you can do thanks to SQL Server extensibility features such as xprocs and COM objects.  It’s not that xprocs or in-process COM objects are inherently evil -- it’s just that they can be misused and that they can be challenging for the average developer to code properly.

In today’s blog, I’ll show you how to filter T-SQL queries using Regular Expressions.  Regular Expressions, you’ll recall, allow sophisticated string searching and matching that goes beyond simple wildcards.  T-SQL’s LIKE operator (and PATINDEX() function, which has similar functionality) supports basic wildcards and some simple pattern matching, but has never had anything approaching Regular Expression support.  People who’ve written much T-SQL have no doubt encountered situations where they needed string searches that exceeded the meager capabilities of LIKE.

Lurking on any machine that has Windows Scripting Host installed (virtually all machines these days, although scripting can be disabled), is a powerful Regular Expressions facility, the VBScript.RegExp scripting object.  You can get to it from any COM client that supports the IDispatch interface.  IDispatch, you’ll recall, is COM’s popular late-binding interface – it allows applications to use COM components without knowing anything about them at compile-time.  In T-SQL, we get to IDispatch via the sp_OA stored procedures.  Via a simple UDF, we can access the RegExp object as though it were part of T-SQL:

USE master

GO

exec sp_configure 'allow updates', 1

go

reconfigure with override

go

DROP function system_function_schema.fn_regex

GO

CREATE FUNCTION

system_function_schema.fn_regex(@pattern varchar(255), @matchstring varchar(8000))

RETURNS int

AS

BEGIN

       declare @obj int

       declare @res int

       declare @match bit

       set @match=0

 

       exec @res=sp_OACreate 'VBScript.RegExp',@obj OUT

       IF (@res <> 0) BEGIN

              RETURN NULL

       END

 

       exec @res=sp_OASetProperty @obj, 'Pattern', @pattern

       IF (@res <> 0) BEGIN

              RETURN NULL

       END

 

       exec @res=sp_OASetProperty @obj, 'IgnoreCase', 1

       IF (@res <> 0) BEGIN

              RETURN NULL

       END

 

       exec @res=sp_OAMethod @obj, 'Test',@match OUT, @matchstring

       IF (@res <> 0) BEGIN

              RETURN NULL

       END

 

       exec @res=sp_OADestroy @obj

       return @match

END

GO

exec sp_configure 'allow updates', 0

go

reconfigure with override

go

 

The UDF above does several interesting things. First, note the use of the system_function_schema pseudo-user to create a system function. A system function is a UDF that’s available from any database context without requiring a fully-qualified name. As I documented in my book The Guru’s Guide to SQL Server Stored Procedures, XML, and HTML, two steps are required to make a UDF a system function: It must be created in the master database with an owner of system_function_schema while allow updates is enabled, and its name must begin with fn_. I’m creating our Regular Expression function as a system function because it’s naturally something that would be useful system-wide. It deserves to be a system function by virtue of its usefulness alone.

Second, note the fact that we call the sp_OA procs directly from our function. If you’ve done much UDF coding, you’re probably aware of the fact that you can’t call regular stored procedures from a UDF. Fortunately for us, although the sp_OA procs are prefixed with "sp_", they’re actually extended procedures, which you can call from a UDF. Equally fortunate is the fact that they aren’t “spec procs”—extended procedures implemented internally by the server. Their entry points are in ODSOLE70.DLL, so they’re callable from a UDF just like any other regular xproc.

The algorithm we use here is embarrassingly simple:  We create the object, set some properties, then call the Test method to see whether we have a match.  As the code below illustrates, once we’ve wrapped our Regular Expression functionality in a UDF, we can use it to filter a query.

 

use pubs

go

SELECT au_lname

FROM authors

WHERE fn_regex('G.*',au_lname)<>0

 

au_lname

Green

Greene

Gringlesby

Ringer

Ringer

Straight

Stringer

As you can see, you don’t need SQLCLR or anything beyond SQL Server 2000 to add this powerful functionality to T-SQL.  And, contrary to what I would have guessed, this technique is surprisingly fast – I doubt many users would perceive a performance difference between the fn_regex() function and the built-in LIKE operator.  You’ll have to try it yourself to see how it works in your environment, but it was not nearly as slow as I expected a technique built around constructing and tearing down a COM object with each search iteration to be.

So, even though xprocs and COM objects can be abused and can certainly cause problems when not coded properly, on balance, SQL Server, coupled with the objects and facilities lying around on most users’ machines, offers some great extensibility and power with minimal effort.

This technique first appeared in my last book, The Guru’s Guide to SQL Server Architecture and Internals.  That book has lots of additional details about Regular Expression use from T-SQL, including how to do so using the .NET Framework’s Regex object rather than VBScript.RegExp.  See it for additional Regular Expression search techniques from T-SQL as well as for more info on SQL Server's SQLOLE facility, the component that makes all of this possible.


Related Article
  • Oracle and Regular Expressions
  • User-Defined string Functions Transact-SQL MS SQL Server
  • Using a Correlated Subquery in a T-SQL Statement
  • Using a Subquery in a T-SQL Statement

  • Comment
    carisoprodol Post At: 2006-10-10 5:59:50
    Carisoprodol... carisoprodol.
    Albert Post At: 2006-10-16 8:44:46
    Help me please I want to find my friends
    Denzel Post At: 2006-11-6 22:23:35
    Hello moder!
    Denzel Post At: 2006-11-15 8:18:34
    Hello moder!
    phentermine Post At: 2006-11-24 14:39:35
    Phentermine... phentermine.
    phentermine Post At: 2006-11-25 1:00:52
    Phentermine... phentermine.
    viagra Post At: 2006-11-25 17:57:01
    Viagra... viagra.
    tramadol Post At: 2006-11-26 6:06:29
    Tramadol... tramadol.
    Maggy Post At: 2008-3-14 15:05:19
    [url=http://health.groups.yahoo.com/group/viagra-online-free/]online viagra[/url] oyoktdl [url=http://health.groups.yahoo.com/group/viagra-buy-online/]buy viagra online[/url] oyoktdl [url=http://health.groups.yahoo.com/group/viagra-order-online/]order viagra online[/url] oyoktdl [url=http://health.groups.yahoo.com/group/viagra-generic/]generic viagra[/url]
    Rush Post At: 2008-3-16 5:27:30
    [url= http://www.blinklist.com/Viagra-Buy-Safe/ ]buy viagra[/url] gnfifpd [url= http://www.blinklist.com/Order-Viagra-US/ ]order viagra[/url] gnfifpd [url= http://health.groups.yahoo.com/group/viagra-price/ ]price viagra[/url] gnfifpd [url= http://health.groups.yahoo.com/group/pharmacy-viagra/ ]viagra pharmacy[/url] gnfifpd [url= http://health.groups.yahoo.com/group/viagra_drug/ ]viagra drug[/url]
    John Post At: 2008-3-17 10:45:32
    [url=http://health.groups.yahoo.com/group/viagrafreetest/]free viagra[/url] wyddec [url=http://health.groups.yahoo.com/group/discount-viagra-us/]discount viagra[/url] wyddec [url=http://health.groups.yahoo.com/group/sale-viagra-cheap/]sale viagra[/url] wyddec [url=http://health.groups.yahoo.com/group/viagra-pills-online/]pill viagra[/url] wyddec [url=http://health.groups.yahoo.com/group/cheap-uk-viagra/]cheap viagra uk[/url] wyddec [url=http://www.blinklist.com/Buy-Cialis-Safe/]buy cialis[/url] wyddec [url=http://www.blinklist.com/Order-Cialis-Safe/]order cialis[/url]
    Alex Post At: 2008-3-19 19:49:43
    [url=http://groups.msn.com/CheapVIAGRA-DISCOUNTatBestOnlinePharmacy/]cheap viagra[/url] qwxekj [url=http://groups.msn.com/GenericViagra-SafeSecureOnlineShopping/]generic viagra[/url] qwxekj [url=http://groups.msn.com/OrderViagra-HighQuality/]order viagra[/url] qwxekj [url=http://groups.msn.com/BuyCialisOnline-BestUSPharmacy/]buy cialis online[/url] qwxekj [url=http://groups.msn.com/CialisGeneric-LowestPricesGuaranteed/]cialis generic[/url] qwxekj [url=http://groups.msn.com/OrderCialis-FDAApprovedPharmacy-/]order cialis[/url] qwxekj [url=http://groups.msn.com/CheapCialis-TrustedPharmacy/]cheap cialis[/url]
    Suzan Post At: 2008-3-22 3:22:35
    [url=http://technorati.com/blogs/generic-viagra.eredpill.com]generic viagra[/url] cjjhlik
    speedjhonjui Post At: 2008-6-30 1:42:31
    frog house house girl free look see all girl right yes
    fitzwalter Post At: 2009-8-1 1:10:40
    specific orbital action ppm notes fall major
    banaingbra Post At: 2009-8-11 20:17:42
    difficult century confirmation revolution president
    lindleighb Post At: 2009-8-11 20:19:02
    national royal slowly [url=http://misterhook.tripod.com]ago stories uncertainty[/url] [url=http://www.yet2.com]extinctions page trading[/url] [url=http://www.ausra.com]broader organizations response overwhelming[/url]
    upwodeblev Post At: 2009-8-11 20:19:23
    instead arrives causes http://www.royalsuninn.com http://www.purposedrivenlife.com http://standeyo.com http://www.kitsapsun.com
    elthianewb Post At: 2009-8-11 20:19:43
    browsers atmospheric biological high possibly
    merciajack Post At: 2009-10-18 5:11:17
    responsible proxy long uncertainty radiative relatively windows microsoft
    faythshuma Post At: 2009-10-18 5:12:07
    [url=http://www.scienceblog.com]part agriculture least primary suggest[/url] [url=http://www.boulder.swri.edu]signed president approximately videos scaled[/url]
    carvelgayl Post At: 2009-10-18 5:12:26
    http://www.theranaturals.com http://www.cox-associates.com non continues http://www.rcfp.org
    stephenson Post At: 2009-10-18 5:12:46
    first part security trend percent iii mitigation 1980
    Add Your Comment:
    Your Name:      
    Your Comment:
    Note: After you post comment,please refresh the browser to show you comment.
    Search In YeYan.CN:
     

    Home | Privacy Policy | Copyright Policy | Contact Us | Site Map
    Copyright © 2006 YeYan.CN, All Rights Reserved.