这里是可能解决您问题的代码。
首先运行以下函数。
CREATE FUNCTION dbo.fnSplit(
@sInputList VARCHAR(8000)
, @sDelimiter VARCHAR(8000) = ' '
) 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
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