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

在sql server中,每个单词在字符串中重复多少次?,sql,sql-server,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 等等 帮我查一下密码。任何帮助都将不胜感激。先谢谢你 编辑: 到目前

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

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', '')).

任何解释以及提供的代码将不胜感激。谢谢。

使用t-sql拆分器,这非常简单。我个人的偏好是Jeff Moden splitter,可以在这里找到。这里还有一些其他的选择

用杰夫·摩登的理论就这么简单

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
如果你想得到所有的技术

;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

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

首先运行下面的函数

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

到目前为止你试过什么?StackOverflow不是代码编写服务。如果您在某些特定代码方面遇到问题,那么大多数人都会乐于帮助您。老实说,我是sql新手。到目前为止,我已经理解了如何找到替换了特定字母表的数字词。我将在编辑中发布代码。@MycrowSoft不用担心,让我试着为您的问题编写一个查询。@TomH谢谢您的链接,我一定会在下次发布问题时仔细阅读并尝试更正自己。@ChiragThakar感谢您的时间。工作很好,谢谢您的时间。@MycrowSoft好的。这是我的荣幸,我总是尽力帮忙。那会有用的,但那种类型的拆分器对性能来说绝对是最差的。@SeanLange当然,我会努力改进它。我很感激你的建议。这里有一些更好的拆分器选择。当您在拆分器中看到光标或while循环时,它将无法很好地缩放。这就是基于集合的操作的本质:你也可以看看我回答中关于理货表的链接。
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