Saturday, January 19, 2008

Using Regular expressions with SQL Server via TSQL

Using Regular expressions with SQL Server via TSQL
TSQL Regular Expression Workbench (simple-talk.com)

Download TSQL Scripts:
RegexWorkbench.sql
RegexWorkbench2000.sql

A great deal can be done using commandline applications that work with regular expressions such as GREP and AWK. However, there are times where it is handy to use Regex directly from TSQL. There are two Regex engines available to SQL Server. These are
the .NET Regex which is in the system.text.regularexpression module
The ECMA Regex from VBScript.RegExp which is distributed with the IE browser and is used by Javascript and JScript.
Both of these are excellent standard implementations. Both work well in TSQL.
The .NET Regex requires the creation of CLR functions to provide regular expressions, and works only with SQL Server 2005, (and 2007) See CLR Integration by Christoffer Hedgate
The ECMA Regex can be used via VBScript.RegExp, which are available to SQL Server 2000 as well. The regex is compatible with Javascript.
The advantage of using CLR is that the regular expressions of the NET framework are very good, and performance is excellent. However, the techniques are well-known, whereas some of the more powerful uses of VBScript.RegExp have hardly ever been published, so this workbench will concentrate on the latter.


See Also:
SQL Home > T-SQL Programming

No comments: