美好的一天,
我为此工作了一个小时,我想在特定列中提取特定单词
这是我的SQL查询
DECLARE @string_of_text NVARCHAR(MAX)='This is sample text that will retrieve all text with CVE-2014-1775, CVE-2014-1779, CVE-2014-1799, CVE-2014-1803, and CVE-2014-2757.'
DECLARE @get_str NVARCHAR(MAX)=''
DECLARE @flag INT=1
DECLARE @move INT=1
WHILE PATINDEX('%CVE-%', SUBSTRING(@string_of_text
,@move,LEN(@string_of_text )))>0
BEGIN
SET @str=SUBSTRING(@string_of_text ,@move,LEN(@string_of_text ))
SET @flag= PATINDEX('%CVE-%', @string_of_text )
SET @get_str = @get_str +','+SUBSTRING(@string_of_text , @flag,13)
SET @move=@flag+13
END
SELECT STUFF(@get_str ,1,1,'') 'CVE LIST'
样本结果
CVE列表 CVE-2014-1775,CVE-2014-1779,CVE-2014-1799,CVE-2014-1803,CVE-2014-2757
答案 0 :(得分:1)
该要求有点含糊,但是,如果您尝试将字符串中存在的一个或多个单词提取到结果集中,则可以使用NGrams8K。请注意以下示例:
DECLARE
@string VARCHAR(8000) = 'This dog barked at that dog then there were two barking dogs',
@searchText VARCHAR(8000) = 'dog';
SELECT ng.position, ng.token
FROM dbo.ngrams8k(@string, LEN(@searchText)) ng
WHERE ng.token = @searchText;
返回:
position token
------------ --------
6 dog
25 dog
57 dog
要对表使用此逻辑,可以执行以下操作:
DECLARE @table TABLE (someId INT IDENTITY, someText VARCHAR(8000));
INSERT @table (someText)
VALUES ('this fish, that fish...'), ('more text containing the word "fish"'),('nothing');
DECLARE @searchText VARCHAR(8000) = 'Fish';
SELECT t.someId, t.someText, ng.position, ng.token
FROM @table t
CROSS APPLY dbo.ngrams8k(t.someText, LEN(@searchText)) ng
WHERE ng.token = @searchText;
返回:
someId someText position token
-------- -------------------------------------- --------- ---------
1 this fish, that fish... 6 fish
1 this fish, that fish... 17 fish
2 more text containing the word "fish" 32 fish