I have a database field with data like the examples below:
FEE 200 A 16 Y N NYFIRE 32.8 C M0008 Y N
INF 150 A 05 Y Y PFE 35 A 05 Y Y
NYFEE 200 A 16 Y N
I need to parse out all the values with an A before them, or any values that are
preceded with an INF. If they are preceded with an INF I need to use those values to deduct them from a formula in SQL. If the values are not preceded with an INF, but are followed by an A I need to add those values in a formula in a stored procedure. IMPORTANT, what looks like spaces in the examples are CHAR(9) characters. There could be up to 9 series of numbers that have an A before them… the above examples only show 1 or 2. I can extract out the first one using a charindex, but don’t know how to get the subsequent A’s out. I can’t get a patindex to work with a ‘%[‘ + CHAR(9) + ‘INF’ + CHAR(9) + ‘]%’, but if I do patindex on just ‘INF’ then I can’t get just the following value with the ‘INF’ as part of the string, I don’t want the ‘INF’ part.
I don’t know how to parse out the values in a stored procedure, or at all (I’ve tried a number of things as mentioned above), and have not been able to find anything except how to parse out data on specific patterns. There is not always the same number of spaces for the value preceding the A, it could be 2 or 3 characters.
This will parse out the values. In SQL 2005/2008 you can do
You can either set up a loop (Which I don’t recommend) or you can do the self joins to figure out what has an FEE and what has an INF and what values you want
If you’re using 2000 you’ll need to set up a split function and loop through your data and call it
The split function is a modified for 2000 version of what is found here by Itai Goldstein