简体   繁体   English

使用T-SQL查找其他列中的列文本

[英]Find text of a column in other column with T-SQL

I have a doubt to find text from one column in a table; 我怀疑是从表的一列中查找文本; in other column from other table. 在其他表格的其他列中。

Imagine that you have this columns: 想象一下,您具有以下列:

在此处输入图片说明

And you want to find the COMPLETE text of [A].X in [B].Y 您想在[B] .Y中找到[A] .X的完整文本。

And to discover where do you have the match. 并发现您在哪里找到比赛。 The colour yellow show this choice: 黄色显示此选择: 在此处输入图片说明

I have been thinking to use the "CONTAINS" function, but I think that it could be not the best idea. 我一直在考虑使用“ CONTAINS”功能,但我认为这可能不是最好的主意。 Because you have to write the text that you need to find (instead of a complete text of a column). 因为您必须编写需要查找的文本(而不是列的完整文本)。

CONTAINS T-SQL 包含T-SQL

I thought that it could be like this: 我认为可能是这样的:

Use AdventureWorks2012;  
GO  
SELECT [B].Y  
FROM Production.Product  
WHERE CONTAINS(([A].Y), [A].X);  

But it doesn't work.

Which is the best option? 哪个是最好的选择?

I am using SQL SERVER V17.0 我正在使用SQL SERVER V17.0

Thanks!!! 谢谢!!!

I would go for like : 我会like

select b.y, a.x 
from b join
     a
     on b.y like '%' + a.x + '%' ;

There is not, however, a really efficient way to do this logic in SQL Server. 但是,没有真正有效的方法可以在SQL Server中执行此逻辑。

Here is a quick example searching a list of strings for a particular set of words defined in another table. 这是一个快速的示例,它在字符串列表中搜索在另一个表中定义的一组特定单词。 This example just searches through the system error messages text and looks for the words 'overflow' and 'CryptoAPI', but you'll substitute the words table with your 'A' and the 'sys.messages' table with your table 'B' 本示例仅搜索系统错误消息文本,并查找单词“ overflow”和“ CryptoAPI”,但是您将单词表替换为“ A”,将“ sys.messages”表替换为表“ B”

NOTE: this isn't the most efficient way to search large amounts of text. 注意:这不是搜索大量文本的最有效方法。

-- CREATE TEMP TABLE WITH WORDS TO MATCH
CREATE TABLE #words (
    [Word] nvarchar(100)
)

-- SAMPLE STRINGS
INSERT INTO #words VALUES ('overflow')
INSERT INTO #words VALUES ('CryptoAPI')

-- SEARCH THROUGH SYSTEM ERROR MESSAGES FOR SAMPLE STRINGS
SELECT [W].[Word] AS 'Matched word'
    , [M].[text]
FROM [sys].[messages] AS [M]
    JOIN #words AS [W]
        ON [M].[text] LIKE '%' + [W].[Word] + '%'
CREATE TABLE #TempA
(ColumnX VARCHAR(10)
);
CREATE TABLE #TempB
(ColumnY VARCHAR(100)
);
INSERT #TempA
VALUES('fish'),('burguer'),('sugar'),('tea'),('coffee'),('window'),('door');

INSERT #TempB
VALUES('I like potatoes'),('I eat sugar'),('I eat sugar with onions'), ('I have a car'),('I don''t like dogs');

SELECT *
FROM #TempB b
WHERE EXISTS(SELECT 1 FROM #TempA a WHERE CHARINDEX(a.ColumnX, b.ColumnY,1) > 0);

I agree with @GordonLinoff. 我同意@GordonLinoff。 Like is what i would also go for but i want to make one improvement in the answer provided. Like是我也是会去的,但我想,在提供了答案一个改进。

CREATE TABLE #TempA (ColumnX VARCHAR(10));
CREATE TABLE #TempB (ColumnY VARCHAR(100));

INSERT #TempA
VALUES ('fish')
    ,('burguer')
    ,('sugar')
    ,('tea')
    ,('coffee')
    ,('window')
    ,('door');

INSERT #TempB
VALUES ('I steam potatoes')
    , ('I like potatoes')
    ,('I eat sugar')
    ,('I eat sugar with onions')
    ,('I have a car coffee')
    ,('I don''t like dogs')
    ,('Window is clean')
    ,('Open the door')
    ;


SELECT b.ColumnY
    ,a.ColumnX
FROM #TempB b
INNER JOIN #TempA a ON ' '+ b.ColumnY + ' ' LIKE '% ' + a.ColumnX + ' %'

This will take care of the TEA to be not found in STEAM. 这将照顾在STEAM中找不到的TEA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM