Results 1 to 19 of 19

Thread: [RESOLVED] SELECT SQL with Word Count

Threaded View

  1. #12
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: SELECT SQL with Word Count

    That can't be right... are you sure you did not make any modifications to my function?

    Here it is again: (I added an extra IF statement to check string length before execution...)
    Code:
    ALTER FUNCTION [dbo].[fn_SplitStringToTable]
    (
    	  @DataList NVARCHAR(MAX)
    	, @Separator NVARCHAR(MAX)
    )
    RETURNS @tbl TABLE (
    	  RowIndex INT PRIMARY KEY
    	, FromPos INT
    	, ToPos INT
    	, ItemData NVARCHAR(MAX)
    )
    AS
    BEGIN
    	-- SELECT * FROM dbo.fn_SplitStringToTable('123,43,5465,6788,1231,111', ',')
    	
    	DECLARE @LenSep INT
    	SET @LenSep = DATALENGTH(@Separator) / 2
    	
    	IF @LenSep > 0 AND DATALENGTH(@DataList) > 0 BEGIN
    		; WITH res (RowIndex, FromPos, ToPos) AS (
    			SELECT CAST(1 AS INT) AS RowIndex
    				, CAST(1 AS INT) AS FromPos
    				, CAST(CHARINDEX(@Separator, @DataList + @Separator) AS INT) AS ToPos
    			
    			UNION ALL
    			
    			SELECT CAST(RowIndex + 1 AS INT) AS RowIndex
    				, CAST(res.ToPos + @LenSep AS INT) AS FromPos
    				, CAST(CHARINDEX(@Separator, @DataList + @Separator, ToPos + @LenSep) AS INT) AS ToPos
    			FROM res
    			WHERE CHARINDEX(@Separator, @DataList + @Separator, ToPos + @LenSep) > 0
    		)
    		INSERT INTO @tbl
    		SELECT res.*, SUBSTRING(@DataList, FromPos, ToPos - FromPos) AS ItemData
    		FROM res
    	END
    	
    	RETURN
    END
    Also, just to double check, what version number shows for the database? (See attached)
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width