Compare two table data using Like Operator:
Scenario: I am having two tables and I need to compare the table data
This one is very very interesting topic, Usually we used to go for cursors to loop through the table data to identify the matching data.
Planned step for this scenario is,
Open a cursor or some loop -> get the data and put it in a dynamic sql -> use like
operator for each row and get the output.
OOPS, I got some different solution/fantastic solution for this problem. We can use inner join to achieve it.
Let's see, how can we achieve it?
drop table VenkatCity
drop table VenkatMainCity
create table VenkatCity(cities varchar(100))
insert into VenkatCity values('Sydney,Melbourne')
insert into VenkatCity values('Chennai,Delhi')
insert into VenkatCity values('Goa,Bombay')
create table VenkatMainCity(cities varchar(100))
insert into VenkatMainCity values('Sydney')
insert into VenkatMainCity values('Chennai')
select * from VenkatCity a
inner join VenkatMainCity b on a.cities like '%'+b.cities+'%'
The above query will take care of matching the records and obtain the desired output.
Scenario: I am having two tables and I need to compare the table data
This one is very very interesting topic, Usually we used to go for cursors to loop through the table data to identify the matching data.
Planned step for this scenario is,
Open a cursor or some loop -> get the data and put it in a dynamic sql -> use like
operator for each row and get the output.
OOPS, I got some different solution/fantastic solution for this problem. We can use inner join to achieve it.
Let's see, how can we achieve it?
drop table VenkatCity
drop table VenkatMainCity
create table VenkatCity(cities varchar(100))
insert into VenkatCity values('Sydney,Melbourne')
insert into VenkatCity values('Chennai,Delhi')
insert into VenkatCity values('Goa,Bombay')
create table VenkatMainCity(cities varchar(100))
insert into VenkatMainCity values('Sydney')
insert into VenkatMainCity values('Chennai')
select * from VenkatCity a
inner join VenkatMainCity b on a.cities like '%'+b.cities+'%'
The above query will take care of matching the records and obtain the desired output.

Cheers,
Venkatesan Prabu .J
No comments:
Post a Comment