Solved

Matching Telephone Numbers To Prefixes

Posted on 2011-09-08
7
407 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:badabing1
Comment Utility
Ok, thanks for your help Dave.
0
 
LVL 5

Expert Comment

by:DavidMorrison
Comment Utility
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
Comment Utility
Question answered regarding performance but script was taken from elsewhere.
0
 

Author Comment

by:badabing1
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn how the fundamental information of how to create a table.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now