Here is a solution by using XML, XQuery, and dynamic SQL.
SQL
USE tempdb;
GO
-- DDL and sample data population, start
DROP TABLE IF EXISTS DELIMTEDPATH;
CREATE TABLE DELIMTEDPATH (ID INT IDENTITY PRIMARY KEY, [Path] VARCHAR(MAX));
INSERT INTO DELIMTEDPATH ([Path]) VALUES
('John|Albert|James'),
('Cricket'),
('Mary|Joseph|Priyanka|Gilbert|Customer|Service|Passenger|MN-1234|MK-5678');
-- DDL and sample data population, end
DECLARE @separator CHAR(1) = '|'
, @CrLf CHAR(2) = CHAR(13) + CHAR(10)
, @nameCounter INT
, @i INT = 1;
SET @nameCounter = (SELECT MAX(LEN([Path]) - LEN(REPLACE([Path], @separator,'')))/COALESCE(NULLIF(LEN(@separator), 0), 1) AS cnt
FROM DELIMTEDPATH) + 1;
DECLARE @SQL NVARCHAR(MAX) =
N';WITH rs AS
(
SELECT ID,
CAST(''<root><r>'' + REPLACE([Path], ''' + @separator + ''', ''</r><r>'') + ''</r></root>'' AS XML) AS xmldata
FROM DELIMTEDPATH
)
SELECT ID' + @CrLf;
WHILE @i <= @nameCounter BEGIN
SET @SQL += ', c.value(''(r[' + CAST(@i AS VARCHAR(3)) + ']/text())[1]'', ''VARCHAR(100)'') AS Name' + CAST(@i AS VARCHAR(3)) + @CrLf
SET @i += 1
END
SET @SQL += 'FROM rs CROSS APPLY xmldata.nodes(''/root'') AS t(c);'
-- just to see it
PRINT @sql;
-- we are ready at this point
EXEC sp_executesql @SQL;
Output
+----+---------+--------+----------+---------+----------+---------+-----------+---------+
| ID | Name1 | Name2 | Name3 | Name4 | Name5 | Name6 | Name7 | Name8 |
+----+---------+--------+----------+---------+----------+---------+-----------+---------+
| 1 | John | Albert | James | NULL | NULL | NULL | NULL | NULL |
| 2 | Cricket | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | Mary | Joseph | Priyanka | Gilbert | Customer | Service | Passenger | MN-1234 |
+----+---------+--------+----------+---------+----------+---------+-----------+---------+
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…