I am trying to determine the word frequency in a column that is a VARCHAR(3000). I am not sure if this is the best data type but the table creation was not in hand. In any case, I have been using the following function (taken from here) to split strings up until this point:
CREATE FUNCTION dbo.Split
(
@RowData nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
Id int identity(1,1),
Data nvarchar(100)
)
AS
BEGIN
Declare @Cnt int
Set @Cnt = 1
While (Charindex(@SplitOn,@RowData)>0)
Begin
Insert Into @RtnValue (data)
Select
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
Set @Cnt = @Cnt + 1
End
Insert Into @RtnValue (data)
Select Data = ltrim(rtrim(@RowData))
Return
END
Usage was as follows:
SELECT s FROM dbo.Split(' ', @description)
It has been working very nicely but now I am getting an error:
The statement terminated. The maximum
recursion 100 has been exhausted
before statement completion.
Does anyone have suggestions on what is a good way of achieving this?
Never mind. Just in case someone else faces the same problem, the following from here works perfect on large strings: