1

I have a column that gives me lines like this:

<?xml version="1.0" encoding="UTF-8"?>
<xSettings>
    <systemPropertyName>BLALBLA</systemPropertyName>
    <minimumAmount>198.00</minimumAmount>
    <closingAmount>198.00</closingAmount>
    <useThisSetting>true</useThisSetting>
    <SystemStep dayAfterPrevious="0">
        <System SystemService="1" minimumAmount="450.00" />
    </SystemStep>
    <SystemStep dayAfterPrevious="8">
        <message />
    </SystemStep>
    <SystemStep dayAfterPrevious="3">
        <block />
    </SystemStep>
    <SystemStep dayAfterPrevious="1">
        <message />
    </SystemStep>
    <SystemStep dayAfterPrevious="7">
        <message />
    </SystemStep>
</xSettings>

All numbers in it are variable and the BLA BLA is variable too. What I want is a select that only gives me

<minimumAmount>198.00</minimumAmount>

though this would be eve better:

198.00

basically, I can't figure out how to use regexp_substr to find a specific string , then return a number just after that may be from 1 to 4 digits and has two decimals after.

1
  • As MT0 says - don't use regular expressions to extract values from xml strings. It's like driving screws with a hammer; it can be done, but we should use a screwdriver. Commented Jan 6, 2017 at 13:55

2 Answers 2

2

Do not use regular expressions to parse XML data - use a proper XML parser:

SELECT x.minimumAmount
FROM   your_table t,
       XMLTable(
         '/xSettings',
         PASSING XMLType( t.your_column )
         COLUMNS minimumAmount NUMBER(5,2) PATH './minimumAmount'
       ) x

Or

SELECT TO_NUMBER(
         EXTRACTVALUE( XMLType( your_column ), '/xSettings/minimumAmount' )
       ) AS minimumAmount
FROM   your_table
Sign up to request clarification or add additional context in comments.

Comments

0

XML data should not be queried using regular expressions, but here:

select
  regexp_substr(col, '<minimumAmount>(.*)</minimumAmount>',1,1,null,1) minimum_amount
from your_table;

One other way is to use regexp_replace like this:

select
  regexp_replace(col, '.*<minimumAmount>(.*)</minimumAmount>.*','\1') minimum_amount
from your_table;

2 Comments

While this will work, MT0 is right: one should not use regexp functions to query xml data.
it seems we'll "see" each other here often, so be warned: I am not always right; actually I am wrong often enough!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.