查找包含SQL表中字符串的单词数

时间:2014-12-18 17:06:32

标签: sql-server sql-server-2008 text-search

我被要求搜索特定字符串并生成一个列表,其中描述字段包含该字符串。不是问题。在这种情况下,我使用了LIKE'%man%'它给出了一组包含' man'的记录。这也会出现“管理”,“管理”,“人类”等等。我想找到一个记录集,其中包含找到的包含的单词列表'人'和发生的次数。这在SQL 2008中是否可行?如果是这样,你能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:0)

您的问题非常模糊,而且您想要输出的内容并不清楚。看看这样的事情是否指向正确的方向。

declare @ValueToFind varchar(20) = 'man'

select * 
    , ABS((DATALENGTH(replace(SomeValue, @ValueToFind, ''))) - DATALENGTH(SomeValue)) / DATALENGTH(@ValueToFind)
from (Values('manage manager manman'), ('management'), ('mankind manfred'), ('none here')) v(SomeValue)

答案 1 :(得分:0)

如何:

CREATE TABLE #temp
(
[Description] varchar(250)
)

INSERT INTO #temp
VALUES
    ('a small step for mankind'),
    ('my manager says'),
    ('you might manage to purchase this'),
    ('try to manage this'),
    ('he was a small man'),
    ('no woman no cry'),
    ('no words can describe')



;
WITH CTE
As
(
    SELECT *, CHARINDEX('man',[Description] ) startPos, CHARINDEX(' ', [Description], CHARINDEX('man',[Description] )) endPos
    FROM #temp
    WHERE [Description] LIKE '%man%'
),
WordCTE
AS
(
    SELECT [Description], 
            CASE 
                WHEN endPos = 0 THEN SUBSTRING([Description], startpos, 100) 
                ELSE SUBSTRING([Description], startpos, endPos - startPos) 
            END AS [Word]
    FROM CTE
)
SELECT [Word], COUNT(*)
FROM WordCTE
GROUP BY [Word]

答案 2 :(得分:0)

我想你想要这样的东西。

create table Words(text varchar(50))

insert into Words Values('man')
insert into Words Values('management')
insert into Words Values('man')
insert into Words Values('management')
insert into Words Values('mankind')
insert into Words Values('manpower')

select temptable.text, count(temptable.text) as 'count'
from
(select text 
from Words
where text like '%man%') as temptable
group by temptable.text

http://www.sqlfiddle.com/#!3/a6cdc/1