我有一个 XML 文档作为 varchar(max) 保存在列中。我想要的文本被包围,<Text> Words I Want</Text>
但这些文本标签有时会重复 4 或 5 次。
如何根据文本标签的数量在同一个文档中循环 x 次?
目前我正在使用它来提取文本的第一部分
DECLARE @first_char nvarchar(10)
DECLARE @second_char nvarchar(10)
SET @first_char = 'xt>';
SET @second_char = '</text>';
SELECT[TestId]
,[SectionId],
SUBSTRING
(
-- column
settings
-- start position
,CHARINDEX(@first_char, Settings , 1) + 3
-- length
,CASE
WHEN (CHARINDEX(@second_char, Settings , 0) - CHARINDEX(@first_char, Settings, 0)) > 0
THEN CHARINDEX(@second_char, Settings, 0) - CHARINDEX(@first_char, Settings, 0) - 3
ELSE 0
END
) AS Response
FROM [B2IK-TestBuilder].[dbo].[Questions]
group by [TestId]
,[SectionId], settings
我知道 Text 标签出现了多少次。
这是保存 varchar(max) 的 xml 文档的示例:
<Settings>
<ShowNotes>true</ShowNotes>
<ShowComment>false</ShowComment>
<TextBefore>From the six safety essentials, a </TextBefore>
<TextAfter>is essential before any work is carried out?</TextAfter>
<Items>
<Item>
<Text>Answer 1</Text>
</Item>
<Item>
<Text>Answer 2</Text>
</Item>
<Item>
<Text>Answer 3</Text>
</Item>
<Item>
<Text>Answer 4</Text>
</Item>
<Item>
<Text>Answer 5</Text>
</Item>
<Item>
<Text>Answer 6</Text>
</Item>
先感谢您。