
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Description:	Split Strings
-- =============================================
CREATE OR ALTER PROCEDURE PROC_SPLITSTRING 
	@string VARCHAR(MAX)=NULL, 
	@seperator CHAR=NULL
AS
BEGIN
	
	SET NOCOUNT ON;

	CREATE TABLE #results
	(
		strings VARCHAR(MAX)
	)

	DECLARE @result VARCHAR(MAX)
	DECLARE @position INT

	WHILE CHARINDEX(@seperator, @string) > 0
	BEGIN
		SELECT @position = CHARINDEX(@seperator, @string)
		SELECT @result = SUBSTRING(@string, 1, @position-1)

		INSERT INTO #results SELECT LTRIM(RTRIM(@result))

		SELECT @string = SUBSTRING(@string, @position+1, LEN(@string) - @position)
	END
	
	INSERT INTO #results SELECT LTRIM(RTRIM(@string))

	SELECT strings as 'Value' FROM #results
	RETURN
END
GO
