0

I have a SQL table with a column containing XML. It's not pretty but it is part of the project and noch changeable.

Now I need to extract a value from a specific element of this XML. Value is a federal state, so the length of the value is variable, what means that

SUBSTRING ( expression ,start , length )

is not an option, as i can't say how long my value will be.

Is there a function that allows to search between two expression, like

FOO ('state>', '</state') 

or anything in that direction?

EDIT:

I'm using MySQL 5.6

Example:

<root>
  <person>
    <name>Michael</name>
    <state>Berlin</state>
  </person>
</root>

my output should be

Berlin

where a different person can have a different state and so length of value is not allways the same.

thanks for your time.

3
  • Show us some sample table data and the expected result - all as formatted text, not images. Commented Jan 16, 2019 at 8:31
  • Maybe it's better to do it in Java, .Net, ... any language you want. Commented Jan 16, 2019 at 8:34
  • Try solution described here stackoverflow.com/questions/30172378/… Commented Jan 16, 2019 at 8:43

2 Answers 2

0

INSTR (str, pattern)

was the solution

and my script to select the value in my string looks like this

SELECT ID, substring(
xml,
instr(xml, "<state>") + 23,
(instr(xml, "</state>") - (instr(xml, "<state>") + 23))
) AS STATE
FROM tablexml 
WHERE xml
LIKE '%value%';
Sign up to request clarification or add additional context in comments.

Comments

0

You can use substring_index():

select substring_index(substring_index(expression, '<state'>, 2), '</state>', -1) as state
from t;

Comments

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.