Like operator through sub query | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Like operator through sub query

Hi, create table t1
(code varchar(5)) insert into t1(code)
values(‘abc’) insert into t1(code)
values(‘xyz’) create table t2
(id int, contents varchar(100)) insert into t2(id, contents)
values(1, ‘test content abc’) insert into t2(id, contents)
values(2, ‘test content def’) insert into t2(id, contents)
values(3, ‘test content xyz’) I want to retrieve data from table "t2" for the contents match with the
code data in t1 table, like I want to select the first and
third rows from t2 table. select * from t2 where contents like (select code from t1) The above sql is invalid, just for the understanding purpose I have
provided it. We have implemented the above requirement by iterating the data
from "t2" table and processed. Is it possible to achieve it in one select statement ? Thank you. Regards,

SELECT whatever
ON t2.contents LIKE ‘%’+t1.code+’%’ But don’t expect overwehelming performance. You should rather normalise your structures.

Frank Kalis
Microsoft SQL Server MVP
Heute schon gebloggt?
Ich unterstuetze PASS Deutschland e.V.
Is your table normalised? As said, if you use Like ‘%’+t1.code+’%’, index wont be used if any Madhivanan Failing to plan is Planning to fail
Hi, Thank you frank, this is what I wanted, thanks madhi for the data model URL. Regards,
But if possible, you have to redesign the table structure Madhivanan Failing to plan is Planning to fail