Return to Snippet

Revision: 45623
at May 5, 2011 04:55 by michanne


Initial Code
CREATE FUNCTION dbo.split_part(@string varchar(max),@deliminator varchar(10))
RETURNS table AS
RETURN ( 
	WITH p as (
	SELECT substring(@string, 1, 
		case charindex(@deliminator, @string) 	
		WHEN 0 	
			THEN len(@string) 
			ELSE charindex(@deliminator, @string) - 1 
		END 
			) as parse_val, 
			charindex(@deliminator, @string) as pos 
	WHERE @string is not null 	
		AND len(@string) > 0 
			
	union all 
	SELECT substring(@string, pos + 1, 
		case charindex(',', @string, pos + 1) 	
			WHEN 0 	
			THEN len(@string) - pos 
			ELSE charindex(',', @string, pos + 1) - pos - 1 
			END ) as parse_val, 
			charindex(',', @string, pos + 1) as pos 
			
	FROM p 
	WHERE pos > 0
	)
	SELECT parse_val FROM p
)
GO

Initial URL

                                

Initial Description

                                

Initial Title
Split a string function

Initial Tags

                                

Initial Language
SQL