This is a follow-up question to this TSQL to search text
My situation is following
declare @vendor as table (vName varchar(max))
insert into @vendor
select *
from
(
values
('Maccro'),
('Accro')
) t (one)
declare @transaction as table (descr varchar(max))
insert into @transaction
select *
from
(
values
('recl Maccro something'),
('lrec Accro Maccro'),
('lrec Maccr0'),
('Maccro indeed'),
('ACCR Accro'),
('Raac else')
) t (one)
I can run the following query
Select
a.descr,
CASE WHEN EXISTS(select * From @vendor b
where Charindex(b.Vname, a.descr) > 0)
then 1 else 0 End [doesContain]
from @transaction a
which returns the following
| descr | doesContain |
| --------------------- | ----------- |
| recl Maccro something | 1 |
| lrec Accro Maccro | 1 |
| lrec Maccr0 | 0 |
| Maccro indeed | 1 |
| ACCR Accro | 1 |
| Raac else | 0 |
But is it possible to see on what value it returns 1 for doesContain, i.e. on what value from @vendor table, Exists returns a match.
My desired output is following
| descr | doesContain | containsWhat |
| --------------------- | ----------- | ------------ |
| recl Maccro something | 1 | Maccro |
| lrec Accro Maccro | 1 | Accro |
| lrec Maccr0 | 0 | |
| Maccro indeed | 1 | Maccro |
| ACCR Accro | 1 | Accro |
| Raac else | 0 | |