Skip to content
Advertisement

How to create a SQL function to parse multiple xml formatted strings?

I am trying to parse a price from a price column in SQL. I have already create a function that will parse the price but it only works if “all” price strings are the same on all items. I realize now that I need a more robust function that can parse out multiple price configuration.

Here is my simple SQL query

Here is my function DBO.parsePrice

Here is the XML string with just one price level.

The function will output 10.00 from the string above.

Here is another example with a max of 6 different price levels

My function was only designed to parse out price level (A). To make things even more complicated the price string can also be configured with different order types rather than price levels.

The new function needs to handle all these different situations. Ideally, I would like to have separate columns for each price break rather than a field that has the prices comma separated.

Any help would be greatly appreciated

Regards,

JC

The above SQL query was just an example for posting the question. My actual query is pulling data from other tables using left joins.

Advertisement

Answer

You can create a TVF returning (level, price)

and use it in the query

EDIT

Following OP remarks this is a version of the TVF which returns 0 for @price which is not numeric

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement