Skip to content
Advertisement

Pivoting a string from a table function in different columns

I have a task to create a table function that receives a string and sorts it into different columns. The string itself may vary, but the columns are supposed to be the same always.

This is how the result set is supposed to look

The string is: '100^TEst¬200^TEst2¬300^Test3', but for example if I add “¬400^Test4” that should be in the result set as well.

Here is what I’ve managed to do so far.

Advertisement

Answer

Firstly, although you can use STRING_SPLIT the order of the value returned by it is not guaranteed. As you define the length of your parameter as 1000 in your function stringSplit1 this means we can safely use a function like DelimitedSplit8K_LEAD which does guarantee the order by providing the ordinal position of each value. This means we can do something like this:

If you want this as a functionm, use an inline table value function, not a multi-line TVF like you have done:

If you want to parametrise your row and column delimiters, then you could do it like this:


Definition of DelimitedSplit8K_LEAD for completeness:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement