﻿
CREATE FUNCTION [dbo].[string_SplitString_ordered]
(
    	@String VARCHAR(MAX) ,
    	@Delimiter	VARCHAR(10)
)
RETURNS @RetTable TABLE(
    	String varchar(MAX),
    	RowNo int
)
AS 
BEGIN
    DECLARE @i INT ,
    		@j INT ,
    		@cnt INT
    SELECT 	@i = 1
    SET @cnt = 1
    WHILE @i <= LEN(@String)
    BEGIN
    	SELECT	@j = CHARINDEX(@Delimiter, @String, @i)
    	IF @j = 0
    	BEGIN
    		SELECT	@j = LEN(@String) + 1
    	END
    	INSERT INTO @RetTable SELECT SUBSTRING(@String, @i, @j - @i), @cnt
    	SET @cnt = @cnt + 1
    	SELECT	@i = @j + LEN(@Delimiter)
    END
    RETURN
END