Sample Header Ad - 728x90

Parse large string into multiple columns/rows

1 vote
1 answer
513 views
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: 1. 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 2. It's possible for Price values to not be encapsulated in brackets, but most are 3. There are values encapsulated in brackets that I don't want to capture 4. Sequence matters. The price value parsed from the left-most side of the string is 1 and so on 5. There are 100,000+ distinct values for the string with significant variation 6. 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.
Asked by Doc (23 rep)
Jan 25, 2023, 08:38 PM
Last activity: Jan 26, 2023, 10:27 AM