I found some related examples here on SO, but not one that for this case specifically. Here goes:
I need to use SQL to parse data sourced from a flat file that is pipe delimited. One of the fields is sub-formatted as follows. My end state is to sum the integers within the field, but my question here is to see of ways to use a SQL SELECT to parse/extract JUST the integers. The pattern of the sub-formatting will always be where the desired integers will be preceded by a tilde (~) and followed by an asterisk (*). The number of sub fields may vary too (my example has 5, but there could more or less). The 4 char TAG name is of no importance.
So here is a sample:
|GADS~55.0*BILK~0.0*BOBB~81.0*HETT~32.0*IGGR~51.0|
From this example, all I would want for processing is the final number of 219. Again, I can work on the sum part as a further step; just interested in getting the numbers.
I know how to handle this quite easily with UNIX tools (ie AWK, sed, etc), but not in SQL. Any suggestions will help! Thanks!
Is it this what you want (it calculates the sum 219)? Sorry for the ugly formatting, but for sure you can make it "pretty" by yourself.
This will come back with 219:
Now the code
additional approach using recursive cte:
test is HERE
I have tried to be different.Also I am not following any delimiter. So not following any delimiter may be strength of this technique.
I just extract the first number from string then delete string from 0 till that number and so on.
So it is very clear that we can do it using recursive CTE,while loop and using row_number.
Here I have tried using Recursive CTE which may not be optimize,but sometime it is fun to be different.
This line do the trick for me,
Complete Script,