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.
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%';
You can use substring_index()
:
select substring_index(substring_index(expression, '<state'>, 2), '</state>', -1) as state
from t;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.