Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

This is a split function i have.
SQL
create FUNCTION [dbo].[Split2]
(
    @String NVARCHAR(4000),
    @Delimiter NCHAR(1)
)
RETURNS TABLE 
AS
 

RETURN 
(
    
    WITH Split(stpos,endpos) 
    AS(
        SELECT 0 AS stpos, CHARINDEX(@Delimiter,@String) AS endpos
        UNION ALL
        SELECT endpos+1, CHARINDEX(@Delimiter,@String,endpos+1)
            FROM Split
            WHERE endpos > 0
    )
    SELECT 'Id' = ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
        'Data' =  SUBSTRING(SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos),1,CHARINDEX('$',SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos))-1),
                'UserId' =  SUBSTRING(SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos),CHARINDEX('$',SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos))+1,LEN(SUBSTRING(@String,stpos,COALESCE(NULLIF(endpos,0),LEN(@String)+1)-stpos)))
 
    FROM Split
   
)


..and i tried to run this query

Declare @String1 nvarchar(max);
set @String1='1$1,0$1,2$2,1$2,0$2,3$3,1$3,0$3,4$4,3$4'
SELECT data,UserId FROM dbo.Split2(@String1, ',')
option (maxrecursion 0)


Its working perfectly, but I'm getting this error when i pass more then 4000 characters
SQL
Invalid length parameter passed to the LEFT or SUBSTRING function.


then ,I tried to change
SQL
@String NVARCHAR(4000)
in split function as nvarchar(max);
then it shows
SQL
the maximum allowed (4000).

How do I acheive this, any Idea is highly appreciated. I wanna split string which may be more than 10000 or 20000.
Posted

You can find good peice of information @here[^]

The problems seems here
@String NVARCHAR(4000),


Try changing it to NVARCHAR(MAX)!
 
Share this answer
 
Comments
King Fisher 13-May-15 8:11am    
Thanks Asif,
King Fisher 13-May-15 8:12am    
The question was solved at Stackoverflow
The question was solved at Stackoverflow, Thanks all guys who tried to help me.

http://stackoverflow.com/questions/30212886/how-to-solve-invalid-length-parameter-passed-to-the-left-or-substring-function[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900