1

Say I have a table called Person and a field called name. Data quality in the name field poor. I want to strip out all the none alpha characters e.g.

select Replace(Replace(Replace(Name,',',''),'&',''),'@') from Person
 where Replace(Replace(Replace(Name,',',''),'&',''),'@') = @Name

@Name is the variable passed in.

Using this there will be lots of Replace statements wrapped around each other. I have two questions:

  1. What characters would you strip out of a name search e.g. so if someone searches for Ian O' John, it will find Ian O John.
  2. Would you use another technique to do this e.g. PadIndex or regular expressions
D Stanley
  • 149,601
  • 11
  • 178
  • 240
w0051977
  • 15,099
  • 32
  • 152
  • 329

1 Answers1

0

You can do this.

CREATE FUNCTION [db].[StringNonAlphaCharacters](@Temp VARCHAR(1000))
Returns VARCHAR(1000)
AS
Begin

    Declare @KeepValues as VARCHAR(50)
    Set @KeepValues = '%[^a-z]%'
    While PatIndex(@KeepValues, @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex(@KeepValues, @Temp), 1, '')    
    Return @Temp
End
Anish Shah
  • 7,669
  • 8
  • 29
  • 40