SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 Enhancement of PARSENAME function
 New Topic  Reply to Topic
 Printer Friendly
Previous Page
Author Previous Topic Topic Next Topic
Page: of 2

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 11/12/2013 :  07:09:12  Show Profile  Visit SwePeso's Homepage  Reply with Quote
This too allowd for multicharacter delimiter
CREATE FUNCTION dbo.fnParseList
(
	@Delimiter VARCHAR(10),
	@Text VARCHAR(MAX)
)
RETURNS @Result TABLE (RowID SMALLINT IDENTITY(1, 1) PRIMARY KEY, Data VARCHAR(8000) NOT NULL)
AS

BEGIN
	DECLARE	@NextPos INT,
		@LastPos INT,
		@Offset SMALLINT = DATALENGTH(@Delimiter);

	SELECT	@NextPos = CHARINDEX(@Delimiter, @Text, 1),
		@LastPos = 1 - @Offset;

	WHILE @NextPos > 0
		BEGIN
			INSERT	@Result
				(
					Data
				)
			SELECT	SUBSTRING(@Text, @LastPos + @Offset, @NextPos - @LastPos - @Offset);

			SELECT	@LastPos = @NextPos,
				@NextPos = CHARINDEX(@Delimiter, @Text, @NextPos + @Offset);
		END

	IF @NextPos <= @LastPos
		INSERT	@Result
			(
				Data
			)
		SELECT	SUBSTRING(@Text, @LastPos + @Offset, DATALENGTH(@Text) - @LastPos)

	RETURN
END



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Previous Page
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000