|
| 1 | +# Implementing Regular Expressions in SQL Server using CLR UDF |
| 2 | +SQL Database don't have built-in support for regular expressions, so the only workaround is to use Regular Expressions that exist in .Net framework and expose them as T-SQL functions. |
| 3 | +This code sample demonstrates how to create CLR User-Defined functions that expose regular expression functionalities that exist in .Net framework. |
| 4 | + |
| 5 | +### Contents |
| 6 | + |
| 7 | +[About this sample](#about-this-sample)<br/> |
| 8 | +[Build the CLR/RegEx functions](#build-functions)<br/> |
| 9 | +[Add RegEx functions to your SQL database](#add-functions)<br/> |
| 10 | +[Test the functions](#test)<br/> |
| 11 | +[Disclaimers](#disclaimers)<br/> |
| 12 | + |
| 13 | +<a name=about-this-sample></a> |
| 14 | + |
| 15 | +## About this sample |
| 16 | +1. **Applies to:** SQL Server 2005+ Enterprise / Developer / Evaluation Edition |
| 17 | +2. **Key features:** |
| 18 | + - CLR |
| 19 | +3. **Programming Language:** .NET C# |
| 20 | +4. **Author:** Jovan Popovic [jovanpop-msft] |
| 21 | + |
| 22 | +<a name=build-functions></a> |
| 23 | + |
| 24 | +## Build the CLR/RegEx functions |
| 25 | + |
| 26 | +1. Download the source code and open the solution using Visual Studio. |
| 27 | +2. Change the password in .pfk file and rebuild the solution in Retail mode. |
| 28 | +3. Open and save SqlClrRegEx.tt to generate output T-SQL file that will contain script that inserts .dll file with the Regex functions, and exposes them as T-SQL/CLR functions. |
| 29 | + |
| 30 | +<a name=add-functions></a> |
| 31 | +## Add RegEx functions to your SQL database |
| 32 | + |
| 33 | +File SqlClrRegEx.sql contains the code that will import functions into SQL Database. |
| 34 | + |
| 35 | +If you have not added CLR assemblies in your database, you should use the following script to enable CLR: |
| 36 | +``` |
| 37 | +sp_configure @configname=clr_enabled, @configvalue=1 |
| 38 | +GO |
| 39 | +RECONFIGURE |
| 40 | +GO |
| 41 | +``` |
| 42 | + |
| 43 | +Once you enable CLR, you can use the T-SQL script to add the regex functions. The script depends on the location where you have built the project, and might look like: |
| 44 | +``` |
| 45 | +--Create the assembly |
| 46 | +CREATE ASSEMBLY SqlClrRegEx FROM 'D:\GitHub\sql-server-samples\samples\features\sql-clr\RegEx\bin\Release\SqlClrRegEx.dll' WITH PERMISSION_SET = SAFE |
| 47 | +GO |
| 48 | +
|
| 49 | +CREATE SCHEMA REGEX; |
| 50 | +GO |
| 51 | +
|
| 52 | +--Create the functions |
| 53 | +CREATE FUNCTION REGEX.MATCH (@src NVARCHAR(MAX), @regex NVARCHAR(4000)) |
| 54 | +RETURNS BIT |
| 55 | +AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledMatch |
| 56 | +GO |
| 57 | +CREATE FUNCTION REGEX.SUBSTRING (@src NVARCHAR(MAX), @regex NVARCHAR(4000)) |
| 58 | +RETURNS NVARCHAR(4000) |
| 59 | +AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledSubstring |
| 60 | +GO |
| 61 | +CREATE FUNCTION REGEX.REPLACE (@src NVARCHAR(MAX), @regex NVARCHAR(MAX), @value NVARCHAR(4000)) |
| 62 | +RETURNS NVARCHAR(MAX) |
| 63 | +AS EXTERNAL NAME SqlClrRegEx.RegEx.CompiledReplace |
| 64 | +GO |
| 65 | +``` |
| 66 | + |
| 67 | +This code will import assembly in SQL Database and add three functions that provide RegEx functionalities. |
| 68 | + |
| 69 | +<a name=test></a> |
| 70 | + |
| 71 | +## Test the functions |
| 72 | + |
| 73 | +Once you create the assembly and expose the functions, you can use regular expression functionalities in T-SQL code: |
| 74 | + |
| 75 | +``` |
| 76 | +IF( REGEX.MATCH('tst123test', '[0-9]+') = 1 ) |
| 77 | + SELECT REGEX.SUBSTRING('tst123test', '[0-9]+'), REGEX.REPLACE('tst123test', '[0-9]+', 'XXX') |
| 78 | +``` |
| 79 | + |
| 80 | +<a name=disclaimers></a> |
| 81 | + |
| 82 | +## Disclaimers |
| 83 | +The code included in this sample is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this sample. |
| 84 | + |
0 commit comments