Click here to Skip to main content
15,064,275 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. I have to join two tables with common words.
I tried the possibilities, but I was advised to use CTE to split sentences to words and then to find each word in another record. But I don't have much knowledge of CTE. Is there anyone can help with this?

What I have tried:

DECLARE @tablea TABLE(Id int, [Text] VARCHAR(250))
DECLARE @tableb TABLE(Id int, Konto int, [Text] VARCHAR(250))

INSERT INTO @tablea(Id, [Text])
VALUES(1, 'Buy of Caffe'),
(2, 'Gebyr service'),
(3, 'Sell of goods'),
(4, 'Pbs salg varer'),
(5, 'Fatktura')

INSERT INTO @tableb(Id, Konto, [Text])
VALUES(1, 1001, 'Betaling for Faktura'),
(2, 1002, 'Buy goods from butik'),
(3, 1003, 'Sell of caffe to butik'),
(4, 1004, 'Salg'),
(5, 1003, 'GOOGLE service')

SELECT ta.Id, ta.[Text], tb.Konto
FROM @tablea ta LEFT JOIN @tableb tb ON PATINDEX('%' + tb.Text + '%', ta.Text) > 0
Updated 12-Dec-20 2:10am
Richard Deeming 11-Dec-20 8:48am
This appears to be the same as the question you posted back in May:
How to join two tables with common word in SQL server[^]
Member 14085040 11-Dec-20 9:15am
Yes. That's correct, but I was advised to use CTE to resolve this issue, therefore I ask for help again
Member 14085040 11-Dec-20 9:31am
I need the following:

Id | Text              | Konto      
1  | Buy of Caffe      | 1003            
2  | Gebyr service     | 1005             
3  | Sell of goods     | 1002    
4  | Pbs salg varer    | 1004         
5  | Fatktura          | 1001

1 solution

Member 14085040 13-Dec-20 9:10am
Hi @Gerry. It is very interesting, but the problem is that they can not be used between two tables, because if we take for example;
FREETEXT(Column name, 'Query Search'). That is, I cann't use column instead of 'Query Search' because it accepts only one value, while column has more than one value. Do you have any ideas about that? Many thanks in advance for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900