Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi guys, this one currently has me stumped :(I have a table with addresses and another table with country names and a country code.I want to look at the addresses and report all the potential country codes that could be in that address. E.g. If the Address is "12 Iceland Way, Perth, New South Wales, Australia"I would want to match that to Iceland, Wales and Australia.Alternatively, I want to list all the country codes and the addresses that contain them.I have no text extenders on the tables and I have no unique key between the tables..Table Structures:Address Table:RecordID int identifier91,1)Address_1 varchar(35)Address_2 varchar(35)Address_3 varchar(35)Address_4 varchar(35)Address_5 varchar(35)Countries Table:CountryCode varchar(8)CountryName varcahr(20)I would really like to avoid cursors as the tables are 850 x 60K rowsTIAObiron
obiron
Starting Member
23 Posts
Posted - 2009-03-18 : 12:14:03
OK,BFI Method (Brute force and Ignorance)
SELECT C.ID, C.Name, X.UniqueID, X.Address FROM tbl_Country CFULL OUTER JOIN( SELECT A.UniqueID, A.Address FROM tbl_Addresses A ) X on 1=1WHERE charindex(' '+C.name+' ',x.address) <> 0ORDER BY x.UniqueID
It can take a long time becuase there is no optimisation on it.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2009-03-19 : 13:07:16
may be this
SELECT RecordID,AddVal,CountryNameFROM(SELECT a.RecordID,a.AddValFROM Address aUNPIVOT (AddVal FOR Address IN ([Address_1],[Address_2],[Address_3],[Address_4],[Address_5]))u)mINNER JOIN Countries cON m.AddVal LIKE '% ' + c.CountryName + ' %'