I'd like to split the result from a query and display the values in separate columns. As an example I get the following result
|Name |
|ABC_DEFG_HIJKL|
|A_B_C |
|A_B_C_D |
I want to split the values by '_' and add them to separate columns. The query result should look something like this
|Name |first |second |third |fourth|
|ABC_DEFG_HIJKL|ABC |DEFG |HIJKL |null |
|A_B_C |A |B |C |null |
|A_B_C_D |A |B |C |D |
So far I can split the result. But for each value, I have a new row. So I just need to merge the results into one row and make a column for each of those rows.
SELECT DP.Name, value
FROM RitopDatenpunkt DP
CROSS APPLY STRING_SPLIT(DP.Name, '_');
|Name |value |
|ABC_DEFG_HIJKL|ABC |
|ABC_DEFG_HIJKL|DEFG |
|ABC_DEFG_HIJKL|HIJKL |
|A_B_C |A |
|A_B_C |B |
|A_B_C |C |
|A_B_C_D |A |
|A_B_C_D |B |
|A_B_C_D |C |
|A_B_C_D |D |
I know that I should use PIVOT. But what aggragate function do I use and are the arguments for the FOR statement right
SELECT DP.Name, value
FROM RitopDatenpunkt DP
CROSS APPLY STRING_SPLIT(DP.Name, '_')
PIVOT
(
GROUPING(Name) as Name
FOR value in ([first],[second],[third],[fourth])
)piv;