Please check following SQL query
;with cte as (
select
[name],
id,
val
from names
cross apply dbo.split([name],' ')
), mydata as (
select
[name],
case when id = 1 then val end as name1,
case when id = 2 then val end as name2,
case when id = 3 then val end as name3,
case when id = 4 then val end as name4,
case when id = 5 then val end as name5
from cte
)
select
[name],
max(name1) name1,
max(name2) name2,
max(name3) name3,
max(name4) name4,
max(name5) name5
from mydata
group by [name]
I used a user-defined SQL split function, you can find the source codes in referred document
Even though you could use SQL STRING_SPLIT function introduced with SQL Server 2016, since it does not return the order like the referred split function it will not be useful in the above pivot-like query
To visualize the output please check following result set
