I have the following Split
function,
ALTER FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))
returns @tempt
This will be a much faster solution when your string has 1000 or more values to split. For table-valued functions, to have any ordering, you must apply "ORDER BY" at the place of use. This is because "SELECT" from a table without "ORDER BY" is by convention not having any sorting.
CREATE FUNCTION [dbo].[Split]
(
@String VARCHAR(max),
@Delimiter VARCHAR(max)
)
RETURNS @Data TABLE
(
[Order] INT IDENTITY(1,1),
[Value] VARCHAR(max)
)
AS
BEGIN
DECLARE @x XML = cast('' + replace(@String, @Delimiter, '') + '' AS XML)
INSERT INTO @Data
SELECT v.value('.', 'varchar(max)') FROM @x.nodes('i') AS x(v)
RETURN
END
GO