如何在 sql 中拆分两个字符或分隔符的字符串

How to split string on two characters or delimiters in sql

我试图首先在“_”上拆分此字符串“1,1_5,2_3,4”,然后在“,”上拆分子字符串,所以它会像"1,1_5,2_3,4" 首先在 '_' 上拆分,因此它将给出这个子字符串 "1,1" 然后这个子字符串在 ',' 上拆分,然后像这样

将拆分后的子字符串“1 1”插入临时 table
INSERT INTO [dbo].[TEST] ([X],[Y])
                  VALUES (@X,@Y)   

我只用一个分隔符“,”来做这件事,就像这样 首先我创建一个 table 拆分函数

ALTER FUNCTION [dbo].[Split]
(
    -- Add the parameters for the function here
        @RowData nvarchar(2000),
        @SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
    Id int identity(1,1),
    Data nvarchar(100)
)
AS
BEGIN
    -- Declare the return variable here
    Declare @Cnt int
    Set @cnt = 1

    While(charindex(@SplitOn,@RowData) > 0)
    Begin
        Insert Into @RtnValue (data)
        Select
            Data = ltrim(RTRIM(SUBSTRING(@RowData,1,CHARINDEX(@SplitOn,@RowData)-1))) 

            set @RowData = SUBSTRING(@RowData,CHARINDEX(@SplitOn,@RowData)+1,len(@RowData))
            set @Cnt = @Cnt + 1
    End
        insert into @RtnValue(data)
        select Data =  ltrim(RTRIM(@RowData))
    RETURN

END

然后我创建这个存储过程

ALTER PROCEDURE [dbo].[uspTest]
    -- Add the parameters for the stored procedure here
    @StringOFXIDs nvarchar(2000)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    DECLARE @SOMETABLE TABLE(ID INT IDENTITY(1,1) UNIQUE, XID INT);
    DECLARE @XCOUNT INT;
    DECLARE @XCURRENT INT;
    DECLARE @XCOUNTER INT = 1;
    -- Insert statements for procedure here
    INSERT INTO @SOMETABLE([XID])
    SELECT [Data] FROM [dbo].[Split](@StringOFXIDs,',');

    SELECT @XCOUNT = COUNT(1) FROM @SOMETABLE;
    WHILE (@XCOUNTER <= @XCOUNT)
    BEGIN 
        SELECT @XCOUNTER = [XID]
        FROM @SOMETABLE
        WHERE [ID]  = @XCOUNTER

        INSERT INTO [dbo].[TEST] ([X])
                          VALUES (@XCOUNTER)
        SELECT @XCOUNTER +=1;
    END
END

--EXEC [dbo].[uspTest] '1,2,3,4'

然后执行此存储过程,一切正常,但我不知道如何将字符串拆分为两个字符或分隔符,然后插入到临时 table 提前感谢您的帮助。

您可以使用 XML 功能拆分成行,然后拆分成列:

DECLARE @string VARCHAR(100) = '1,1_5,2_3,4'
;WITH cte AS (SELECT RTRIM(LTRIM(Split.a.value('.', 'VARCHAR(100)'))) AS Txt
                     ,ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS RN    
              FROM  (SELECT CAST ('<M>' + REPLACE(@string, '_', '</M><M>') + '</M>' AS XML) AS DATA
                    ) AS A 
                     CROSS APPLY Data.nodes ('/M') AS Split(a)
              )
     ,cte2 AS (SELECT CONVERT(XML,'<String><Section>'+ REPLACE(REPLACE(Txt ,'|',','),',', '</Section><Section>') + '</Section></String>') AS Txt
                      ,RN
               FROM cte
               )   
 SELECT Txt.value('/String[1]/Section[1]','varchar(100)') AS Col1
       ,Txt.value('/String[1]/Section[2]','varchar(100)') AS Col2
FROM  cte2
ORDER BY RN

输出:

Col1       Col2
---------------
1             1
5             2
3             4