Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Group By row format / regex in SQL?

Let’s say I have different formats of phone numbers stored as strings in my database:

Phone
(812) 283-2013
701 857 7000
207-623-0389
5592369797
...

Is it possible to list a sample of each format that I have, so I would know which formats exist?

I’m using SQL Server but I’m interested in the general idea, so the approach for any other relational DB is fine.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If I understand correctly, you are looking for distinct patterns of phone numbers. If so, translate() would be a good option

Declare @YourTable Table ([Phone] varchar(50))  Insert Into @YourTable Values 
 ('(812) 283-2013')
,('701 857 7000')
,('207-623-0389')
,('5592369797')
 

Select distinct Frmat = translate(Phone,'0123456789','##########') 
 from @YourTable

Results

Frmat
### ### ####
##########
###-###-####
(###) ###-####
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading