I have a column with a "Payload" of data. It represents a dynamic field where an application pulls all of the data from a source (API, stored procedure, etc) into one column in one table. Each payload can be a different set of columns, so I cannot land the information into a table. But I need my team to be able to pull out the information. The delimiters are static. Example: Source table looks like this
ID Payload
123 {"Transaction":"123456","Transaction2":"789123"}
124 {"Transaction":"123457","Transaction2":"789124"}
I would like the final output to be:
ID Transaction Transaction2
123 123456 789123
124 123457 789124
I have a split function that I can pass through twice to get me the following:
ID SplitID Split SplitID2 Split 2
123 1 transaction:123456 1 transaction
123 1 transaction:123456 2 123456
123 2 transaction2:789123 1 transaction2
123 2 transaction2:789123 2 789123
124 1...
So now I need to flatten this without using dynamic SQL... OR putting this in a place where a team of 20 can consume and use on a regular basis with NO persisting tables, etc...
Edit: We currently have SQL 2012. We are a few months out from 2017. Sorry for the lack of documenting.
If that document would be slightly more polished for a JSON and you run SQL Server 2016+..:
If you can try RegexAssembly
I'm getting the sense that you are not on 2016+
SQL Server is declarative by design, so getting variable columns without going dynamic is not possible.
If you have a maximumn number of columns, consider the following:
Example
Returns
If you have SQL Server 2016, you can use
json_value
:Example at SQL Fiddle.
For earlier versions of SQL Server, you'd need a CLR UDF, like JSON Select.
If you DON'T have SQL Server 2016 or above, then the answer is a bit more complicated. What I would do is stop after the first split (don't split the name and the value into separate rows), so your table looks like:
...one transaction/row, one row/transaction. Assuming that the split did remove the quotes, as it appears, then something like the following should work (untested):
...but yeah, if you have 2016 or higher, go with the JSON route mentioned in other answers. :)