如何使用 like 运算符检查一个列值是否存在于另一列值中

How to check if one column value exists in another column value using like operator

我试图在另一列中寻找一列的模式并尝试提取数字。

我的table:

ID        aggregatedID
-----------------------
12345     null
234567    null 
34567     null 
316645    null
ID_666    12345@20200131.000@@@|234567@20200131.000@@@|34567@20200131.000@@@|316645@20200131.000@@@

在我的 table 中,前 4 个 ID 存在于 aggregatedID 中,具有特殊字符连接。我必须获取 ID,即 ID_666 并更新前 4 个 ID,其中 aggregatedId 使用不同的列。

这是我的预期输出:

ID        aggregatedID    Agg_ID
--------------------------------
12345     null            ID_666   
234567    null            ID_666 
34567     null            ID_666  
316645    null            ID_666
ID_666    12345@20200131.000@@@|234567@20200131.000@@@|34567@20200131.000@@@|316645@20200131.000@@@

我尝试使用子字符串 .i.e.在“|”之后提取数字在“@”之前+“@”之前的第一个数字

SUBSTRING(aggregatedID,CHARINDEX('|',aggregatedID)+1,LEN(aggregatedID))

但这 returns 第一个 '|' 之后的整个字符串。

我尝试使用 like 将 aggregatedID 与 ID 匹配,但未获得任何数据。

select ID, aggregatedId 
from table m
where m.aggregatedID like'%' + ID + '%'

任何人都可以帮我解决我所缺少的吗?

你必须使用 self inner join

select A.id , B.id from Tab1  A INNER JOIN 
(select * from Tab1 where agg_txt is NOT NULL ) B on B.agg_txt like '%'+ rtrim(A.id) + '%'  ;

Demo