Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

how to compare two varchar columns (join) by elimination a part of data please

$
0
0

-- My first Data


create table #myfirst

(id int,city varchar(20))



insert into #myfirst values

(500,'Newyork')


insert into #myfirst values

(100,'Ediosn')


insert into #myfirst values

(200,'Atlanta')


insert into #myfirst values

(300,'Greenwoods')


insert into #myfirst values

(400,'Hitchcok')


insert into #myfirst values

(700,'Walmart')


insert into #myfirst values

(800,'Madida')



-- My Second Data


create table #mySecond

(id int,city varchar(20),Sector varchar(2))



insert into #mySecond values

(1500,'Newyork','MK')


insert into #mySecond values

(5500,'Ediosn','HH')


insert into #mySecond values

(5060,'The Atlanta','JK')


insert into #mySecond values

(7500,'The Greenwoods','DF')


insert into #mySecond values

(9500,'Metro','KK')


insert into #mySecond values

(3300,'Kilapr','MK')


insert into #mySecond values

(9500,'Metro','NH')



--Third Second Data


create table #myThird

(id int,city varchar(20),Sector varchar(2))



insert into #myThird values

(33,'Walmart','PP')


insert into #myThird values

(20,'Ediosn','DD')


select f

.*,s.Sector  from #myfirst f join#mySecond s on f.city=s.city



/*

id city Sector

500 Newyork MK

100 Ediosn HH

*/



i have doubt on two things

1

)How Can i compare the City names,by eliminating 'The 'at the begining(if thereisanyinsecond tale city)betweenfirstandsecond

2

)after comparing firstandsecond if thereisno match foundinsecond them want to compare with third table values for those notfound



--i tried below to solve first doubt, it is working but want to know any otherwasys to do it please


select f

.*,s.Sector  from #myfirst f join#mySecond s onreplace(f.city,'THE ','')= replace(s.city,'THE ','')



--Expected results wull be



create table #ExpectResults

(id int,city varchar(20),Sector varchar(2))



insert into #ExpectResults values

(200,'Atlanta','JK')


insert into #ExpectResults values

(100,'Ediosn','HH')


insert into #ExpectResults values

(300,'Greenwoods','DF')


insert into #ExpectResults values

(500,'Newyork','MK')


insert into #ExpectResults values

(700,'Walmart','PP')


insert into #ExpectResults values

(800,'Madidar','')



ID city Sector

200 Atlanta JK

100 Ediosn HH

300 Greenwoods DF

500 Newyork MK

700     Walmart PP

800 Madidar


Thnak you in advance

Miln


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>