I have a test function which would sanitize phone nos and allow only nos and characters “x” or “X” to be stored. I have it to where it does most of it other than it allows multiple x’s which I don’t want. Can anybody help me add it to the regular expression also let me know if you spot potential issues ?
CREATE Function [dbo].[RemoveAlphaCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin
While PatIndex('%[^0-9,x,X]%', @Temp) > 0
Set @Temp = Stuff(@Temp, PatIndex('%[^0-9,x,X]%', @Temp), 1, '')
Return @TEmp
End
The problem with
PATINDEXhere is that it can’t really determine that the pattern should change after it hits a string for the first time. So maybe this approach will be simpler:Results: