根据模式将一列拆分为多个列值,例如 (a) (b)?

Split a column into multiple column values based on pattern e.g (a) (b)?

我在根据 (*) 拆分此列时遇到问题

Column x
I (a)Some Text (b)Some more Text (c) Text
I (a)Some Text (b)Some more Text (c) Text

我需要这样的输出;

Col 1 Col 2 Col 3
I (a)Some Text (b)Some more Text (c) Text

是的,在 (a) 之前也有一个随机的 I,但希望我们能以某种方式忽略...

假设有一些独特的东西来标识每一行,您可以 PIVOT 基于 CROSS APPLYSTRING_SPLIT():

DECLARE @x table
(
  [Id] int IDENTITY(1,1),
  [Values] nvarchar(4000) -- this is a terrible column name
);

INSERT @x([Values]) VALUES
  (N'I (a)Some Text (b)Some more Text (c) Text'),
  (N'nonsense (a) mort (b) splungy (c) blat');

;WITH x AS 
(
  SELECT Id, [value] = N'(' + f.[value], 
    rn = ROW_NUMBER() OVER (PARTITION BY x.Id ORDER BY f.[value])
  FROM @x AS x
  CROSS APPLY STRING_SPLIT(x.[Values], N'(') AS f
  WHERE f.[value] LIKE N'_)%'
)
SELECT [1], [2], [3] FROM x
PIVOT (MAX(value) FOR rn IN ([1],[2],[3])) AS p;

结果:

1               2                   3
------------    -----------------   --------
(a)Some Text    (b)Some more Text   (c) Text
(a) mort        (b) splungy         (c) blat

db<>fiddle

这里 a slightly different one returns Id 和问题中的确切列 headers。