I have some XML content in a single field; I want to split each xml field in multiple rows.
The XML is something like that:
<env>
<id>id1<\id>
<DailyProperties>
<date>01/01/2022<\date>
<value>1<\value>
<\DailyProperties>
<DailyProperties>
<date>05/05/2022<\date>
<value>2<\value>
<\DailyProperties>
<\env>
I want to put everything in a table as:
ID DATE VALUE
id1 01/01/2022 1
id1 05/05/2022 2
For now I managed to parse the xml value, and I have found something online to get a string into multiple rows ( like this ), but my string should have some kind of delimiter. I did this:
SELECT
ID,
XMLDATA.X.query('/env/DailyProperties/date').value('.', 'varchar(100)') as r_date,
XMLDATA.X.query('/env/DailyProperties/value').value('.', 'varchar(100)') as r_value
from tableX
outer apply xmlData.nodes('.') as XMLDATA(X)
WHERE ID = 'id1'
but I get all values without a delimiter, as such:01/10/202202/10/202203/10/202204/10/202205/10/202206/10/202207/10/202208/10/202209/10/202210/10/2022
Or, as in my example:
ID R_DATE R_VALUE
id01 01/01/202205/05/2022 12
I have found out that XQuery has a last()
function that return the last value parsed; in my xml example it will return only 05/05/2022
, so it should exists something for address the adding of a delimiter. The number of rows could vary, as it could vary the number of days of which I have a value.
2条答案
按热度按时间ee7vknir1#
请 尝试 以下 解决 方案 。
我 不得 不 修正 XML 以 使 其 格式 良好 。
中 的 每 一 个
| 标识 符|日期|价值 观|
| - -| - -| - -|
| 识别 码 1| 2022 年 1 月 1 日|一 个|
| 识别 码 1| 2022 年 5 月 5 日|2 个|
brc7rcf02#
伊扎克比我快了2分钟。尽管如此,我还是得到了以下信息:
退货: