I am trying to reconstruct a database query I created for MySQL in Microsoft SQL Server. I am looking for an operator or function SQL Server which acts like REGEXP
.
Here is an example of how I am using the operator:
select *
from musicdetails
WHERE artistname REGEXP '^".mysql_escape_string($_GET['search'])."$'
Unlike MySQL and Oracle, SQL Server databases don't support built-in RegEx functions. However, SQL Server offers the following built-in functions to tackle such complex issues: LIKE. PATINDEX.
MySQL supports another type of pattern matching operation based on the regular expressions and the REGEXP operator. It provide a powerful and flexible pattern match that can help us implement power search utilities for our database systems. REGEXP is the operator used when performing regular expression pattern matches.
Regular expressions are a concise and flexible notation for finding and replacing patterns of text. A specific set of regular expressions can be used in the Find what field of the SQL Server Management Studio Find and Replace dialog box.
In tools, such as SSMS, you can specify the regex patterns in the Find What and Find and Replace options.
Here you go (compile as SQL CLR assembly):
using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[SqlFunction]
public static bool RegexMatch(string expr, string regex)
{
return Regex.IsMatch(expr, regex);
}
[SqlFunction]
public static string RegexReplace(string expr, string regex, string replace)
{
return Regex.Replace(expr, regex, replace);
}
[SqlFunction(FillRowMethodName="GetToken",
TableDefinition="Value nvarchar(max)")]
public static IEnumerable RegexSplit(string expr, string regex)
{
return Regex.Split(expr, regex);
}
public static void GetToken(object row, out string str)
{
str = (string) row;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With