简体   繁体   English

在SQL中从String提取值

[英]Extracting value from String in SQL

I have a SQL table with a column containing XML. 我有一个SQL表,其中包含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. 现在,我需要从该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 我正在使用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) INSTR(str,模式)

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() : 您可以使用substring_index()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM