在sql server中的字符串中重复每个单词多少次?

时间:2016-01-26 18:56:12

标签: sql sql-server

我已经声明了一个字符串,如下所示:

Declare @string
Set @string = 'this line is like this because this is repeated these many times in this line'

我正在尝试找出每个单词重复多少次。我期待像

这样的结果
Word   Number
this    x times
line    y times
is      z times

依旧......

帮我处理代码。任何帮助,将不胜感激。提前谢谢。

编辑:

到目前为止,我已经找到了替换特定字母的数字。

以下是代码

SELECT Len(@string) - Len(Replace(@string, 'x', '')).

将非常感谢任何解释以及提供的代码。谢谢。

3 个答案:

答案 0 :(得分:4)

;WITH splitString(val) AS       
(
    -- convert the string to xml, seperating the elements by spaces
    SELECT    CAST('<r><i>' + REPLACE(@string,' ','</i><i>') + '</i></r>' AS XML)
)
SELECT  [Key],
        COUNT(*) [WordCount]
FROM    (   -- select all of the values from the xml created in the cte
            SELECT  p.value('.','varchar(100)') AS [Key]
            FROM    splitString
                    CROSS APPLY val.nodes('//i') t (p)) AS t
GROUP BY [Key]

如果你想获得所有技术......

;WITH splitString(val) AS       
(
    -- convert the string to xml, seperating the elements by spaces
    SELECT    CAST('<r><i>' + REPLACE(@string,' ','</i><i>') + '</i></r>' AS XML)
)
SELECT  Word,
        CAST(COUNT(*) AS VARCHAR) 
            + (CASE WHEN COUNT(*) = 1 THEN ' time' ELSE ' times' END) AS  Number
FROM    (   -- select all of the values from the xml created in the cte
            SELECT  p.value('.','varchar(100)') AS Word
            FROM    splitString
                    CROSS APPLY val.nodes('//i') t (p)) AS t
GROUP BY Word

答案 1 :(得分:2)

使用t-sql拆分器这非常简单。我个人的偏好是Jeff Moden分离器,可以在这里找到。 http://www.sqlservercentral.com/articles/Tally+Table/72993/此处还有其他一些选项。 http://sqlperformance.com/2012/07/t-sql-queries/split-strings

使用Jeff Moden就是这么简单。

Declare @string varchar(1000)
Set @string = 'this line is like this because this is repeated these many times in this line'

select x.Item
    , COUNT(*) as WordCount
from dbo.DelimitedSplit8K(@string, ' ') x
group by x.Item
order by x.Item

答案 2 :(得分:1)

以下是可以解决您问题的代码。

首先运行以下功能

CREATE FUNCTION dbo.fnSplit(

 @sInputList VARCHAR(8000) -- List of delimited items

, @sDelimiter VARCHAR(8000) = ' ' -- delimiter that separates items

 ) RETURNS @List TABLE (item VARCHAR(8000))

 BEGIN

 DECLARE @sItem VARCHAR(8000)

 WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0

 BEGIN

 SELECT

 @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,

    @sInputList,0)-1))),

 @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,

    @sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))

 IF LEN(@sItem) > 0

 INSERT INTO @List SELECT @sItem

 END

 IF LEN(@sInputList)> 0

 INSERT INTO @List SELECT @sInputList -- Put the last item in

 RETURN

 END

现在运行此代码

Declare @string Varchar(MAX)
Set @string = 'this line is like this because this is repeated these many times in this line'

DECLARE @Words AS TABLE(
ID INT IDENTITY(1,1),
Words VARCHAR(50))

INSERT INTO @Words
select * from fnSplit(@string, ' ')

SELECT Words AS Word, COUNT(*) Number FROM @Words GROUP BY Words ORDER BY Number DESC