I have a table with a column with strings like these:
/TYPE/BOOKING/IBAN/NL12BANK0003456789/BIC/BANKNL2A/NAME/Mr. A. Someguy/CODE/Codenumber 12345678/REF/NOTPROVIDED/LINE/ABCD EFG 234567890 1234 ETC
/TYPE/BOOKING/IBAN/NL34BANK000123456/BIC/BANKNL2U/NAME/Mr. A. Dinges/CODE/98765432/REF/NOTPROVIDED
And I want to look up individual elements in these strings without having to write unreadable code with many CHARINDEX-es and SUBSTRINGS. So I found the SPLIT_STRING function.
select contract, [value]
from SCHEMA.PAYMENTS
CROSS APPLY STRING_SPLIT(paymentrow, '/')
It's great, but now I get these values in rows:
bookingnumber value
12-3-56789012-3
12-3-56789012-3 TYPE
12-3-56789012-3 BOOKING
12-3-56789012-3 IBAN
12-3-56789012-3 NL12BANK0003456789
12-3-56789012-3 BIC
12-3-56789012-3 BANKNL2A
12-3-56789012-3 NAME
12-3-56789012-3 Mr. A. Someguy
12-3-56789012-3 CODE
12-3-56789012-3 Codenumber 12345678
12-3-56789012-3 REF
12-3-56789012-3 NOTPROVIDED
12-3-56789012-3 LINE
12-3-56789012-3 ABCD EFG 234567890 1234 ETC
98-7-65431234-0
98-7-65431234-0 TYPE
98-7-65431234-0 BOOKING
98-7-65431234-0 IBAN
98-7-65431234-0 NL34BANK000123456
98-7-65431234-0 BIC
98-7-65431234-0 BANKNL2U
98-7-65431234-0 NAME
98-7-65431234-0 Mr. A. Dinges
98-7-65431234-0 CODE
98-7-65431234-0 98765432
98-7-65431234-0 REF
98-7-65431234-0 NOTPROVIDED
So as a final step, I would to pivot the value column so that the elements in the original string appear as neat columns, like this:
bookingnumber type IBAN BIC name code ref
12-3-56789012-3 BOOKING NL12BANK0003456789 BANKNL2A Mr. A. Someguy Codenumber 12345678 NOTPROVIDED
98-7-65431234-0 BOOKING NL34BANK0001234567 BANKNL2U Mr. A. Dinges 98765432 NOTPROVIDED
I've been tinkering with PIVOT, but either it doesn't work, or it doesn't give the right results.
Help would be greatly appreciated.
