Creating CLR SQL User Defined Function to Validate Values Using Regular Expression.

This article will explain on how to validate your values using a regular expression in a user defined SQL function. SQL server does not provide any function for this purpose but we can write a CLR enabled function to fulfill our purpose.

 

Step1: Write code in .Net

 

The following code will create a .Net SQL function that will validate values on the basis of a regular expression. Create a new Class library project with the name ClrFunctions. Add a new class (Functions) to the project and copy / paste the code below to the class file.

 

using System.Text.RegularExpressions;

using Microsoft.SqlServer.Server;

using System.Data.SqlTypes;

 

namespace ClrFunctions

{

    public partial class