Solved

Matching Telephone Numbers To Prefixes

Posted on 2011-09-08
7
411 Views
Last Modified: 2012-05-12
I have a requirement to match telephone numbers to a prefix within a table of prefixes.  In the instance where there are multiple matches the longest number match should be selected.

Example:
Prefix
0845
08456
08457
084568
084567

0845685746 should match 08456
0845456875 should match 0845

My question is should I perform this task in a VB.net script in the SSIS package which imports the list of calls or should I stage the calls and then run a SQL script to do the matching?

Any help with the script would also be much appreciated.  I am looking for the best performance solution as this may need to deal with millions of calls per import.

Thanks in advance for any help.
0
Comment
Question by:badabing1
  • 4
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
DavidMorrison earned 500 total points
ID: 36502318
If you are after performance do it in SQL, SSIS is slower but you get a much more "row by row" approach, that's the trade off.


the sql should look something like this:

with AllMatches
as
(
      select P.Prefix, len(P.Prefix) as PrefixLen, T.TelephoneNo
      from TelephoneNos as T
      JOIN Prefixes as P on P.Prefix = left(T.TelephoneNo, len(P.Prefix))
)


select AM.Prefix, AM.Telephone
from AllMatches as AM
where AM.PrefixLen = (select max(X.PrefixLen) from AllMatches as X where X.TelephoneNo = AM.TelephoneNo)


thanks

Dave
0
 

Author Comment

by:badabing1
ID: 36509624
Thanks for the response Dave.
I will focus on doing this task in SQL then for performance.  I have been given a neat piece of code, will this be comparible to your script in terms of performance?  In this script the CLI table is being updated with the Prefix table's foreign key, where there is not a match -1 is returned.
UPDATE a
	SET idPrefix = ISNULL((
	SELECT TOP 1 idPrefix
	FROM Prefix AS b
	WHERE a.CLI LIKE b.Prefix + '%'
	ORDER BY LEN(b.Prefix) DESC),-1)
FROM CLI AS a

Open in new window

0
 
LVL 5

Expert Comment

by:DavidMorrison
ID: 36510241
thats a tidy bit of code, I'd run them side by side to compare performance. I wouldn't imagine there will be much in it either way depending on indexing etc


Thanks

Dave
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:badabing1
ID: 36510264
Ok, thanks for your help Dave.
0
 
LVL 5

Expert Comment

by:DavidMorrison
ID: 36510269
actually, looking at it, the code you have supplied will not be able to use any indexes as the LIKE statement and the string concatenation break the SARGability

If you're tables are correctly indexed I would think my code would run faster



Thanks

Dave
0
 

Author Closing Comment

by:badabing1
ID: 36510308
Question answered regarding performance but script was taken from elsewhere.
0
 

Author Comment

by:badabing1
ID: 36510326
I had just closed the issue when I noticed your additional comment, I have awarded you the full points anyway for the solution.  I will consider using your script over the one I posted above following your last comment.  Thanks.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Outsource Your Fax Infrastructure to the Cloud (And come out looking like an IT Hero!) Relative to the many demands on today’s IT teams, spending capital, time and resources to maintain physical fax servers and infrastructure is not a high priority.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question