I have a string column that I need to parse into multiple different columns and rows.
Sample string value:
If the total charge exceeds {$10,000.00} ,pricing is recalculated to be a {50.00}% discount off charges, not to exceed an average daily charge of {$1,000.00}{2,3}
Desired output:
Price Price_Type Sequence
===== ========== ========
10000.00 Dollar 1
50.00 Percent 2
1000.00 Dollar 3
A few things to note:
- I'm only looking to parse Price values that have either '$' or '%' attached to it. '$' is within the brackets before the Price value, '%' is outside the brackets and after the Price value
- It's possible for Price values to not be encapsulated in brackets, but most are
- There are values encapsulated in brackets that I don't want to capture
- Sequence matters. The price value parsed from the left-most side of the string is 1 and so on
- There are 100,000+ distinct values for the string with significant variation
- I'm using SQL Server 2017
I'm trying to avoid tedious 100+ lines of CASE statements. I'm guessing the solution will involve a recursive function, recursive CTE, table-valued function, or some combination of these. My attempts at this have not gotten far.
There are essentially two looping mechanisms that need to occur: one to get all rows for a single string and another to go through all the distinct strings.