How to make Regex work in SQL 2005?

Hi I need to use Regex function in SQL 2005, but I got the following message after I ran the query like
select ROUTINE_NAME
from INFORMATION_SCHEMA.ROUTINES
where ROUTINE_TYPE = N'PROCEDURE'
    and dbo.RegexMatch( ROUTINE_NAME,
        N'^usp_(Insert|Update|Delete|Select)([A-Z][a-z]+)+$' ) = 0
-------------------------------------------------------------------------------------------------------------------------------
Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.RegexMatch", or the name is ambiguous.
-----------------------------------------------------
So sth is missing? I have to install CLR? How to do that? I was following the article below.
http://msdn.microsoft.com/msdnmag/issues/07/02/SQLRegex/default.aspx

Thanks a lot!
qinyanAsked:
Who is Participating?
 
nmcdermaidConnect With a Mentor Commented:
I beleive the process is:

1. Compile your .Net code to a DLL
2. Create an assembly refencining the DLL, using CREATE ASSEMBLY
3. Create a function called RegexMatch which references the assembly

 

This help reference:

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/864b393f-225f-4895-8c8d-4db59ea60032.htm

Has this example of how to create a CLR UDF:


DECLARE @SamplesPath nvarchar(1024);
-- You may have to modify the value of the this variable if you have
--installed the sample someplace other than the default location.
SELECT @SamplesPath = REPLACE(physical_name, 'Microsoft SQL Server\MSSQL.1\MSSQL\DATA\master.mdf', 'Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\')
    FROM master.sys.database_files
    WHERE name = 'master';

CREATE ASSEMBLY [SurrogateStringFunction]
FROM @SamplesPath + 'StringManipulate\CS\StringManipulate\bin\debug\SurrogateStringFunction.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO

CREATE FUNCTION [dbo].[len_s] (@str nvarchar(4000))
RETURNS bigint
AS EXTERNAL NAME [SurrogateStringFunction].[Microsoft.Samples.SqlServer.SurrogateStringFunction].[LenS];
GO


0
 
ddrudikCommented:
I suspect you mean to use Regex.Match but that's just a guess.
0
 
qinyanAuthor Commented:
In general I want to use the Regular Expression funtion in SQL 2005. For now I can say it's right, I want to use Regex.Match function. But in order to use it i have to first compile the code in Figure 1 from the article and then use create an assembly right? I'm not a .net developer so I don't know how to do that. I just want to know the steps as the article already had the code out there so I want to test it.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
ddrudikCommented:
Can't help with that, sorry.
0
 
qinyanAuthor Commented:
Thanks very much for the help!
I ran the create assembly statement below but got this message:
Msg 6501, Level 16, State 7, Line 2
CREATE ASSEMBLY failed because it could not open the physical file "C:\Temp\CLR\ClassLibrary1\bin\Release\ClassLibrary1.dll": 3(The system cannot find the path specified.).
-------------------------------------------------------------------------------------------------------------------------
CREATE ASSEMBLY MyRegexTest
FROM 'C:\Temp\CLR\ClassLibrary1\bin\Release\ClassLibrary1.dll'    
WITH PERMISSION_SET = EXTERNAL_ACCESS;
----------------------------------------------------------------------------------------------------------------
so where is the problem? Does the path have to be under Program Files\Microsoft SQL Server...?
Thanks again!
0
 
qinyanAuthor Commented:
oK I figured it out the c drive is on my local and the sql server is a remote server. Anyway I was able to create the assembly and I'm testing the function...I'll see how it works. Thanks!
0
 
qinyanAuthor Commented:
Well, when I create the function I got this message:
Msg 6505, Level 16, State 1, Procedure RegexMatch, Line 2
Could not find Type 'Microsoft.Samples.SqlServer.MyRegexTest' in assembly 'ClassLibrary1'.
--------------------------------------------------------------------------------------------------------------------------
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000))
RETURNS bigint
AS EXTERNAL NAME [MyRegexTest].[Microsoft.Samples.SqlServer.MyRegexTest].[RegexMatch];
GO
0
 
qinyanAuthor Commented:
This is the .net code I compiled to dll:
-------------------------------------------------------------------------------------
public static partial class UserDefinedFunctions
{
    public static readonly RegexOptions Options =
        RegexOptions.IgnorePatternWhitespace |
        RegexOptions.Singleline;

    [SqlFunction]
    public static SqlBoolean RegexMatch(
        SqlChars input, SqlString pattern)
    {
        Regex regex = new Regex( pattern.Value, Options );
        return regex.IsMatch( new string( input.Value ) );
    }
}
-----------------------------------------------------------------------------
And I was trying to test the function by running this query see whether the input string matches the pattern specified.
select dbo.RegexMatch( N'123-45-6789', N'^\d{3}-\d{2}-\d{4}$' )

Thank you!
0
 
qinyanAuthor Commented:
OK, OK, I figured it out:
CREATE FUNCTION [dbo].[RegexMatch] (@str nvarchar(4000), @pattern nvarchar(4000))
RETURNS bit
AS EXTERNAL NAME [MyRegexTest].[ClassLibrary1.UserDefinedFunctions].[RegexMatch];
GO

then the query below returns 1.
select dbo.RegexMatch( N'123-45-6789', N'^\d{3}-\d{2}-\d{4}$' )


Thanks a lot!
0
 
nmcdermaidCommented:
Excellent, now we all know how to do it! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.