I found this SQL split string function on internet, but when I pass a string contains 700+ items in it, separated by commas, it only generate a table variable with 280 rows, can some help me to identify where's the problem?
The code:
ALTER FUNCTION [dbo].[fn_Split](@text nvarchar(MAX), @delimiter varchar(20) = ' ')
RETURNS @Strings TABLE
(
position int IDENTITY PRIMARY KEY,
value nvarchar(MAX)
)
AS
BEGIN
DECLARE @index int
SET @index = -1
WHILE (LEN(@text) > 0)
BEGIN
SET @index = CHARINDEX(@delimiter , @text)
IF (@index = 0) AND (LEN(@text) > 0)
BEGIN
INSERT INTO @Strings VALUES (@text)
BREAK
END
IF (@index > 1)
BEGIN
INSERT INTO @Strings VALUES (LEFT(@text, @index - 1))
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
ELSE
SET @text = RIGHT(@text, (LEN(@text) - @index))
END
RETURN
END
This is the code I used to test it:
SELECT * FROM fn_Split(@string,',');
@String is nvarchar(MAX) contains 700+ items, but it only returns a table with 280 rows.
LRMWEB_fn_Split, yet the code shown above defines a function calledfn_Split. Are you sure you're calling the correct function? Furthermore, how long is your @string-parameter? Note that nvarchar(MAX) cannot hold more than 4000 characters.a,a,a,a,a,a,a ...? Just to see if it still cuts off after 280 items.