sqlite 解析特定XML标记值

atmip9wb  于 2023-04-30  发布在  SQLite
关注(0)|答案(1)|浏览(210)

我有SQLite 338.2列中有XML标记的表:

with cte(xml_tag) as (values 
  ('<Event time="Sat Apr 22 1:01:51.887" type="Debug" thread="2164: Main CIM worker thread" elapsed="1" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.883" type="Debug" thread="2164: Main CIM worker thread" elapsed="23" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.874" type="Debug" thread="2164: Main CIM worker thread" elapsed="456" function="Geodatabase.Cursor" code="EndCursor">'), 
  ('<Event time="Sat Apr 22 1:01:51.846" type="Debug" thread="2164: Main CIM worker thread" elapsed="7890" function="Geodatabase.Cursor" code="EndCursor">'))
select * from cte

db<>fiddle
我想从elapsed标记中提取值作为数字:

elapsed
-------
      1
     23
    456
   7890

如何使用SQL查询从XML标记中提取值?

a2mppw5e

a2mppw5e1#

假设所有xml_tag的值都包含子字符串'elapsed='(只有一次),你可以使用字符串函数:

SELECT SUBSTR(xml_tag, INSTR(xml_tag, 'elapsed=') + LENGTH('elapsed=') + 1) + 0 AS elapsed
FROM cte;

通过将0添加到提取的字符串中,它被隐式地转换为一个数字。
参见demo

相关问题