SQL Split Function and Ordering Issue?

后端 未结 5 1133
小鲜肉
小鲜肉 2021-01-18 03:53

I have the following Split function,

ALTER FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))     
                returns @tempt         


        
5条回答
  •  旧巷少年郎
    2021-01-18 04:19

    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
    

提交回复
热议问题