Examples

Current Articles | Categories | Search

By Jonathan Kehayias @ Saturday, April 05, 2008 1:18 AM :: 1617 Views :: 2 Comments
This example covers creating a generic user defined function to allow the use of regular expressions in SQL Server for pattern matching. This can then replace the pattern matching in tsql.  This function can be used as the basis for custom check constraints on table columns as well.

 
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;

public partial class RegexFunctions
{
    [SqlFunction(IsDeterministic = true, IsPrecise = true)]
    public static bool RegExMatch(string pattern, string matchString)
    {
        Regex r1 = new Regex(pattern.TrimEnd(null));
        return r1.Match(matchString.TrimEnd(null)).Success;
    }
}

 
To use the above function you will need to have an existing Regular Expressions pattern to test with.  You can use this to validate that an IP Address is valid with the following regex pattern:

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

DECLARE @pattern varchar(100)
SET @pattern = '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
SELECT dbo.RegExMatch(@pattern, '10.254.100.12')
SELECT dbo.RegExMatch(@pattern, '999.999.999.999')

 


Posted @ Saturday, April 05, 2008 1:18 AM by Jonathan Kehayias
Previous Page | Next Page
Comments
By Sankar Reddy on Saturday, April 05, 2008
Jonathan,
Its a nice idea to have something like this. Code snippet with black background is not very readable on the page. Probably it will be nice to think about changing to a aesthetic looking color for reading. I am look forward to see more activity here.

Sankar Reddy

By Site Administrator on Saturday, April 05, 2008
Sankar,

Thank you for the feedback. I am still working on getting a complete skin up, and resolving how I want the css to layout so that the sections format out better. I have adjusted this to use a table to contain the source code and removed the background coloring. Hopefully this will make this easier to read.

You must be logged in to post a comment. You can login here

Survey

Which of the following CLR objects are you currently using in SQL Server?




Submit Survey  View Results

Links

  Search

What's New

 Subscribe in a reader

Q&A with Adam Machanic - Who's Afraid of SQLCLR by Jonathan Kehayias

You could almost hear the gasps of horror when Microsoft integrated the .NET Common Language Runtime (CLR) into SQL Server 2005. Run C# or VB.NET code inside SQL Server? For many database developers and administrators, it was like an alien had just burst through SQL Server’s midsection.

But SQL Server MVP Adam Machanic says that more and more developers and DBAs are discovering that far from destroying SQL Server performance and security, SQLCLR is actually a powerful ally in solving complex business and technical problems.

Read More on the PASS website..

SQLCLR String Splitting Part 2: Even Faster, Even More Scalable by Site Administrator

SQL Server MVP Adam Machanic shows a new way of parsing strings in SQL Server using SQLCLR that outperforms all conventional TSQL methods, as well as the most common SQLCLR implemenations, using a custom split function that allows streaming of the results.

Trading in xp_cmdshell for SQLCLR (Part 1) - List Directory Contents by Jonathan Kehayias

Learn how to use SQLCLR to get file system information instead of using xp_cmdshell on your SQL Servers.

Recent Examples


Copyright 2007 by SQLCLR.net Terms Of Use Privacy Statement
Website graphics provided by Matt Green Designs
Page generated in 0.2028052 seconds.

All information and example code on this site is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from its use.

This site is in no way affiliated with Microsoft. Unless specifically stated otherwise, nothing should be construed to represent the official positions or opinions of Microsoft and/or its Employees.