如果您有大量数据,这将需要很长时间,但我怀疑使用 SQL 是否有许多更好的替代方案。
DECLARE @Table TABLE (TableID INT PRIMARY KEY, ColumnX VARCHAR(32))
INSERT INTO @Table VALUES (1, '(Info 1) (Info 2)');
INSERT INTO @Table VALUES (2, '(Info 1)');
INSERT INTO @Table VALUES (3, '(Info 10) (Info 20)');
INSERT INTO @Table VALUES (4, '(Info1')
INSERT INTO @Table VALUES (5, '(Info1) (Info2')
INSERT INTO @Table VALUES (6, '(Info1) Info2)')
INSERT INTO @Table VALUES (7, 'Info1')
INSERT INTO @Table VALUES (8, 'Info1)')
INSERT INTO @Table VALUES (9, NULL);
SELECT
TableID
, [Column1] = CASE WHEN PATINDEX('%(%)%', ColumnX) = 1
THEN SUBSTRING(ColumnX
, CHARINDEX('(', ColumnX) + 1
, CHARINDEX(')', ColumnX)
- CHARINDEX('(', ColumnX) - 1
)
ELSE NULL END
, [Column2] = CASE WHEN PATINDEX('%(%)%(%)%', ColumnX) = 1
THEN SUBSTRING(ColumnX
, CHARINDEX('(', ColumnX, CHARINDEX('(', ColumnX) + 1) + 1
, CHARINDEX(')', ColumnX, CHARINDEX(')', ColumnX) + 1)
- CHARINDEX('(', ColumnX, CHARINDEX('(', ColumnX) + 1) - 1
)
ELSE NULL END
FROM @Table