Comparing phone numbers sql

萝らか妹 提交于 2019-12-19 21:46:50

问题


I have 2 sets of phone numbers in 2 different tables, table 1 has the straight forward format of 01234567890 and the other table has 3 different formats, sometimes it's 01234567890 or 01234 567890 or 01234-567890.

Currently I'm just doing an Inner join on the tables and only get a few rows returned but would expect more as obviously anything with a hyphen or space will get missed from the join.

The way the system is I can't change the data in the table with 3 formats so can't standardise it or clean it out etc.

What would be the best way to tackle this?


回答1:


Well you could think about creating a computed column in your second table to normalize the phone number format - something like:

ALTER TABLE dbo.YourSecondTable
ADD NormalizedPhone AS REPLACE(REPLACE(PhoneColumn, '-', ''), ' ', '') PERSISTED

This expression removes any spaces and any dashes from the PhoneColumn and those values are stored in a new, computed column called NormalizedPhone.

This column will always be kept up to date - even if you change your PhoneColumn's value later on. It will always contain the normalized phone number automagically.

Now you can easily join the two tables on the normalized phone strings, and you should get more accurate results.



来源:https://stackoverflow.com/questions/15655813/comparing-phone-numbers-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!