Question

I have a database table which stores web transactions for a fundraising donation page. One of the columns in the table stores a bunch of concatenated data in the format 'data1=value1 data2=value2 data3=value3 moredata4=value3..."

The pairs represent fields/columns from an external database integrated thru a rest service. God knows why it's stores in this way, but I need to parse this column into multiple columns. I cannot figure out how to do this in an efficient and clean way.

Something to note: not all records/rows in the table have a value for every item in the combined column. So you might see 'data1=value1 data2= data3=data3...' also not all 'firlds/columns' in the concatenated pairs start with 'data', some might say 'tranamt=50 coupon=2bb2' for example. Also to note, the 'space' looking character between pairs is actually chr(10) not a space.

I'd really appreciate anyone's help on this. I'm stumped. I've researched this endlessly on stackexchange and Google, and not been able to find anything that helps me with this unique scenario. I've split delimited columns before, but not with a 'field/column' and value pair. I'm also new to oracle. This is in an oracle db.

Thanks!

Was it helpful?

Solution

dbfiddle

There surely are, a lot of, Oracle experts who can write more optimized code (in Oracle) than this fiddle.... 😉

OTHER TIPS

I'd access this first at the point where those data are created - replace whatever mechanism you use (looks like self-built using carriage-return chr(10) as divider ) - by a proper json serialization - which enables simple json de-serialization

To parse the existing data in said column - you can use SUBSTR https://www.techonthenet.com/oracle/functions/substr.php together with INSTR https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_1103.htm#OLADM564 functions of Oracle - but I suggest you do it outside the database in some programming language of your choice

first substr up to the first occurance of chr(10) .. and so forth - problem is to get oracle to accept "data" as column-names . hence the suggestion to do it outside the database in a program / script

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top