Technology Musings
Face it: data cleanup is a fact of life. While SQL Server has a handful of string manipulation functions, nothing even comes close to the power of RegEx. Fortunately, by leveraging the CLR functionality in SQL Server 2005 and SQL Server 2008, we can add a host of new features, including regular expressions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.Text.RegularExpressions; public partial class UserDefinedFunctions { [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)] public static SqlString RegExMatch(SqlString expression, SqlString pattern) { if (expression.IsNull || pattern.IsNull) return SqlString.Null; Match match = new Regex(pattern.ToString()).Match(expression.ToString()); return match.Success ? new SqlString(match.Value) : SqlString.Null; } }; |
We’re going to use a simple regular expression to check for a valid US postal code (AKA Zipcode + 4):
^\d{5}(-\d{4})?$
This regular expressions checks for exactly 5 digits followed by an option group of hyphen and 4 more digits.
SELECT dbo.RegExMatch('90210','^\d{5}(-\d{4})?$') , dbo.RegExMatch('90210-1234','^\d{5}(-\d{4})?$') , dbo.RegExMatch('90210-','^\d{5}(-\d{4})?$') , dbo.RegExMatch('9021A','^\d{5}(-\d{4})?$')
And results:
90210 90210-1234 NULL NULL
If you need to change the target database, do so in the project’s properties:
Thanks to Jeff’s SQL Server Blog for the initial Regular Expression Replace code.