Link to home
Start Free TrialLog in
Avatar of qinyan
qinyan

asked on

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!
Avatar of ddrudik
ddrudik
Flag of United States of America image

I suspect you mean to use Regex.Match but that's just a guess.
Avatar of qinyan
qinyan

ASKER

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.
Can't help with that, sorry.
ASKER CERTIFIED SOLUTION
Avatar of nmcdermaid
nmcdermaid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of qinyan

ASKER

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!
Avatar of qinyan

ASKER

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!
Avatar of qinyan

ASKER

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
Avatar of qinyan

ASKER

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!
Avatar of qinyan

ASKER

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!
Excellent, now we all know how to do it! :)