Implementation steps
1: Create a database project
2: Refer to the relevant library, simple implementation code as follows
Using system;using system.data;using system.data.sqlclient;using system.data.sqltypes;using Microsoft.sqlserver.server;using system.text.regularexpressions;public Partial class regexsql{ [ Microsoft.SqlServer.Server.SqlFunction] public static SqlBoolean regex_ismatch (string input, String Regex) { return new SqlBoolean (Regex.IsMatch (input, Regex, regexoptions.ignorecase)); } [Microsoft.SqlServer.Server.SqlFunction] public static SqlString Regex_match (string input, String Regex) { return new SqlString (Regex.match (Input, regex , Regexoptions.ignorecase). Value);} }
3: Add an assembly in MSSQL
If it fails, execute the script first
exec sp_configure ' show advanced options ', ' 1 '; goreconfigure;goexec sp_configure ' clr enabled ', ' 1 ' goreconfigure;exec sp _configure ' Show advanced Options ', ' 1 '; Go ALTER DATABASE [db_name] Set trustworthy on;go
4: Create an association function
Create function Regex_match (@input as nvarchar, @regex as nvarchar) returns nvarchar ( +) as External name iftrue_regex.regexsql.regex_match;gocreate function regex_ismatch (@input as nvarchar (+), @regex as nvarchar (+)) returns Bitas external name Iftrue_regex.regexsql.regex_ismatch;go
5: Using functions to implement regular matching
SELECT dbo. Regex_ismatch (' abcde123asdf234 ', ' [b-s]+ ') select dbo. Regex_match (' abcde123asdf234 ', ' [b-s]+ ')
Note: The above implementation is based on framework3.5, in the framework4.0 to add the assembly error, the same way can implement many of the database itself is inconvenient to implement functions, such as: Call Web API, WebService, etc.
Code download
Using VS2013 to create a database project implementing the MSSQL regularization method