vick vick - 1 month ago 14
SQL Question

SQL Server query using regex?

I have a

field in a SQL Server database that stores phone numbers in many different ways, but they are all phone number essentially.


(818) 123 4564
818 - 123 - 4567

I was hoping I can use regex to strip out all non-numeric characters and then perform a like or "=" on .. can I do that?

forgot to mention: I only have read access.


If you know that the field contains a phone number in some kind of valid form, then the following really ugly usage of LIKE would match a specific number. To find 818-123-4567:

select * from thetable where phonenum like ('%8%1%8%1%2%3%4%5%6%7%')

This, of course, would match invalid entries as well (e.g., numbers that had extra digits, characters, etc.). And it would likely be a fairly expensive query unable to use any indexes.

A more realistic version might be this:

select * from thetable where phonenum like ('%818%123%4567%')