I'm looking how to replace/encode text using RegEx based on RegEx settings/params below:
RegEx.IgnoreCase = True
RegEx.Global = True
RegEx.Pattern = "[^a-z\d\s.]+"
I have seen some examples on RegEx, but confused as to how to apply it the same way in SQL Server. Any suggestions would be helpful. Thank you.
Or some other expression instead of A-Z
You can use a function in SqlServer and pass to it the value you want to evaluate. Have a look at this link : http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx
You will have to build a CLR procedure that provides regex functionality, as this article illustrates.
Their example function uses VB.NET:
...and is installed in SQL Server using the following SQL (replacing '%'-delimted variables by their actual equivalents:
You do not need to interact with managed code, as you can use LIKE:
As your expression ends with
+
you can go with'%[^a-z0-9 .][^a-z0-9 .]%'
EDIT: to make clear: SQL Server doesn't supports regular expressions without managed code. Depending on the situation, the
LIKE
operator can be an option, but it lacks the flexibility that regular expressions provides.Regular Expressions In SQL Server Databases Implementation Use
Regular Expression - Description
. Match any one character
* Match any character
+ Match at least one instance of the expression before
^ Start at beginning of line
$ Search at end of line
< Match only if word starts at this point
> Match only if word stops at this point
\n Match a line break
[] Match any character within the brackets
[^...] Matches any character not listed after the ^
[ABQ]% The string must begin with either the letters A, B, or Q and can be of any length
[AB][CD]% The string must have a length of two or more and which must begin with A or B and have C or D as the second character
[A-Z]% The string can be of any length and must begin with any letter from A to Z
[A-Z0-9]% The string can be of any length and must start with any letter from A to Z or numeral from 0 to 9
[^A-C]% The string can be of any length but cannot begin with the letters A to C
%[A-Z] The string can be of any length and must end with any of the letters from A to Z
%[%$#@]% The string can be of any length and must contain at least one of the special characters enclosed in the bracket
Slightly modified version of Julio's answer.
You'll need Ole Automation Procedures turned on in SQL: