Using regular expressions in SQL Server

Source: Internet
Author: User

I. New. NET Class Library project

  1. Create a class library project named Mssqlregexextend
  2. Create a class named Regexextend
  3. Copy the following code into the class[CSharp]View PlainCopy
    1. Using System.Text.RegularExpressions;
    2. Namespace Mssqlregexextend
    3. {
    4. public class Regexextend
    5. {
    6. // <summary>
    7. /// regular match
    8. // </summary>
    9. /// <param name= "regex" > Regular Expressions </param>
    10. /// <param name= "input" > Text </param>
    11. // <returns></returns>
    12. [Microsoft.SqlServer.Server.SqlFunction]
    13. public static string Match (string regex, string input)
    14. {
    15. return string. IsNullOrEmpty (input)? " ": new Regex (Regex, regexoptions.ignorecase). Match (input).  Value;
    16. }
    17. // <summary>
    18. /// regular replacement
    19. // </summary>
    20. /// <param name= "regex" > Regular Expressions </param>
    21. /// <param name= "input" > Text </param>
    22. /// <param name= "Replace" > Target to replace </param>
    23. // <returns></returns>
    24. [Microsoft.SqlServer.Server.SqlFunction]
    25. public static string replace (string regex, string input, string replace)
    26. {
    27. return string. IsNullOrEmpty (input)? " ": new Regex (Regex, regexoptions.ignorecase).  Replace (input, replace);
    28. }
    29. // <summary>
    30. /// regular check
    31. // </summary>
    32. /// <param name= "regex" > Regular Expressions </param>
    33. /// <param name= "input" > Text </param>
    34. // <returns></returns>
    35. [Microsoft.SqlServer.Server.SqlFunction]
    36. public static bool IsMatch (string regex, string input)
    37. {
    38. return! string. IsNullOrEmpty (input) && new Regex (Regex, regexoptions.ignorecase).  IsMatch (input);
    39. }
    40. }
    41. }
  4. Right-click the project Build

Second, register the class library in MSSQL

Execute the following script in the database (the Class inventory drop address has to be modified correctly).

[SQL]View PlainCopy
  1. --drop ASSEMBLY Regex
  2. CREATE ASSEMBLY Regex from ' E:\CSharp\MSSQLRegexExtend\MSSQLRegexExtend\bin\Release\MSSQLRegexExtend.dll ' with Permission_set = SAFE --Register the. NET class Library
  3. sp_configure ' clr enabled ', 1 --Set the database to use CLR components
  4. RECONFIGURE --Set the available CLR components. Don't forget to run this line to apply
  5. /**** The following code registers a static method in the class library as a function ****/
  6. /**** regular Match ****/
  7. --drop FUNCTION [dbo]. [Regex.match]
  8. CREATE FUNCTION [dbo].[ Regex.match] (@Regex [nvarchar] (max), @Input [nvarchar] (max))
  9. RETURNS [nvarchar] (max) with EXECUTE as CALLER
  10. As
  11. EXTERNAL NAME [regex].[ Mssqlregexextend.regexextend]. [Match]
  12. /**** Regular Replacement ****/
  13. --drop FUNCTION [dbo]. [Regex.Replace]
  14. CREATE FUNCTION [dbo].[ Regex. Replace] (@Regex [nvarchar] (max), @Input [nvarchar] (max), @Replace [nvarchar] (max))
  15. RETURNS [nvarchar] (max) with EXECUTE as CALLER
  16. As
  17. EXTERNAL NAME [regex].[ Mssqlregexextend.regexextend]. [Replace]
  18. /**** Regular Check ****/
  19. --drop FUNCTION [dbo]. [Regex.IsMatch]
  20. CREATE FUNCTION [dbo].[ Regex.IsMatch] (@Regex [nvarchar] (max), @Input [nvarchar] (max))
  21. RETURNS [Bit] with EXECUTE as CALLER
  22. As
  23. EXTERNAL NAME [regex].[ Mssqlregexextend.regexextend]. [IsMatch]

Iii. Invocation of the example

[SQL]View PlainCopy
  1. SELECT [CustomerID]
  2. , [CompanyName]
  3. , [ContactName]
  4. , [ContactTitle]
  5. , [City]
  6. , [Region]
  7. , [PostalCode]
  8. , [country]
  9. , [Phone]
  10. , [Fax]
  11. , [Address]
  12. , [dbo]. [Regex.match] (' (\d) + ', [Address]) as [house number] --Regular match
  13. , [dbo]. [Regex. Replace] (' \d ', [Address],' * ') as [code the house number] --Regular replacement
  14. From [Northwind]. [dbo]. [Customers]
  15. where [dbo]. [Regex.IsMatch] (' \d ', [Address]) =1 --Check the record of the house number

Using regular expressions in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.