Question

I'm working on SQL Server 2012.

I would like to split the different items from the Table1 to compare with a specific column of the Table2.

Table1 have a row like that :

|   id    |           items            | 
|   1     |  aaa;ery;sha;cbre;dezrzyg; |
|   2     |  aaa;ery;sha;cbre;dezrzyg; | // Could be the same items than another row
|   3     |  dg;e3ry;sd6ha;cb8re;48dz; |
|   4     |  e5zeza;48;dz;46az;12BREd; |
|  ...    |            ...             |
|   10    |            aaa             | // Currently match because the request compare the whole cell

items is a string (ntext in the db) and the string never contain spaces.

Table2 have a row like that :

|   id    |            item            | 
|   1     |            aaa             | // match
|   2     |            AAA             | // match
|   3     |           aaa52            | // doesn't match
|   4     |           2aaa2            | // doesn't match
|  ...    |            ...             |

item also is a string (nvarchar in the db) and the string never contain spaces.

Here is my current SQL request :

SELECT * FROM Table1 t1
INNER JOIN Table2 t2 ON t1.items = t2.item

How could I solve my problem ? Should I split a string then compare each Table1.items to Table2.item ? Is there something in SQL to resolve it easily ?

Was it helpful?

Solution

Is there something in SQL to resolve it easily ?

No but you can creatively use like. Indexes can not help you with performance when you do something like this.

select *
from Table1 as T1
  inner join Table2 as T2
    on ';'+cast(T1.items as nvarchar(max))+';' like '%;'+T2.item+';%'

SQL Fiddle

OTHER TIPS

The failsafe solution is to split the content of items column into table-like form and then join it to table2.

Say we have these tables:

create table #t1 (id int, items varchar(100));
go
insert #t1 values
( 1, 'aaa;ery;sha;cbre;dezrzyg;'),
( 2, 'aaa;ery;sha;cbre;dezrzyg;'),
( 3, 'dg;e3ry;sd6ha;cb8re;48dz;'),
( 4, 'e5zeza;48;dz;46az;12BREd;'),
(10, 'aaa');
go

create table #t2 (id int, item varchar(100));
go
insert #t2 values
(1, 'aaa'),
(2, 'AAA'),
(3, 'aaa52'),
(4, '2aaa2')
go

We'll use the following approach to split the items:

select substring(items, n, charindex(';', items + ';', n) - n)
from numbers, #t1
where substring(';' + items, n, 1) = ';'
and n < len(items) + 1

This requires a numbers table, see here how to create it.

Here's the whole query:

select distinct #t1.id, #t1.items, case when #t2.id is null then 'doesn''t match' else 'match' end
from #t1
cross apply (
    select substring(items, n, charindex(';', items + ';', n) - n)
    from numbers 
    where substring(';' + items, n, 1) = ';'
    and n < len(items) + 1
) x (col)
left join #t2 on x.col = #t2.item
--where #t2.id is not null
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top