System.Text.RegularExpressions
public static SqlString RegExReplace(SqlString expression, SqlString pattern, SqlString replace) { }
public static SqlString RegExReplace(SqlString expression, SqlString pattern, SqlString replace) { if (expression.IsNull || pattern.IsNull || replace.IsNull) return SqlString.Null; Regex r = new Regex(pattern.ToString()); return new SqlString(r.Replace(expression.ToString(), replace.ToString())); }
CREATE TABLE [dbo].[AddressRef]( [AddressId] [int] IDENTITY(1,1) NOT NULL, [Address1] [varchar](25) NULL, [Address2] [varchar](25) NULL, [City] [varchar](17) NULL, [StateCode] [char](2) NULL, [Zip] [int] NULL, CONSTRAINT [PK_AddressRef] PRIMARY KEY CLUSTERED ( [AddressId] ASC ) )
DECLARE @searchOn varchar(25) DECLARE @address1 varchar(25) DECLARE @add1Num varchar(25) SET @searchOn = LTRIM(RTRIM(rccms.dbo.RegExReplace('445 Lucky St', '[0-9]', ''))) ;WITH a1 AS ( SELECT a.AddressId, a.Address1, 'Diff' = DIFFERENCE(LTRIM(RTRIM(rccms.dbo.RegExReplace(a.Address1, '[0-9]', ''))), @searchOn) FROM AddressRef a WITH (NOLOCK) WHERE 1=1 AND (COALESCE(a.Address1, '') <> '') ) SELECT AddressId, Address1 FROM a1 WHERE 1=1 AND (Diff >= 3)
print SOUNDEX('Easy St.') -- E200 print SOUNDEX('Lucky St.') -- L200