如何拆分包含XML字符串的SQL字段

如何拆分包含XML字符串的SQL字段,sql,sql-server,xml,Sql,Sql Server,Xml,我有一个包含XML片段的字段。我想创建一个#tentable,它将包含XML数据的一部分,但只显示两个特定标记之间的任何字符 字段值如下所示(为便于阅读而包装): 我不知道你指的是哪一个。第一个将它们作为单独的xml行返回,另一个将它们作为类型化数据行返回: DECLARE @sample TABLE ( id INT IDENTITY, xmlSnippet VARCHAR(MAX) ); INSERT @sample ( xmlSnippet ) VALUES ('

我有一个包含XML片段的字段。我想创建一个
#tentable
,它将包含XML数据的一部分,但只显示两个特定标记之间的任何字符

字段值如下所示(为便于阅读而包装):


我不知道你指的是哪一个。第一个将它们作为单独的xml行返回,另一个将它们作为类型化数据行返回:

DECLARE @sample TABLE
(
    id INT IDENTITY,
    xmlSnippet VARCHAR(MAX)
);

INSERT @sample
(
    xmlSnippet
)
VALUES
('<monthdate>2019-07-01</monthdate>
<automitemno>302</automitemno>
<amount>1.190000000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>
<monthdate>2019-07-01</monthdate>
<automitemno>2131</automitemno>
<amount>0.386750000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>');


DECLARE @tempTable TABLE
(
    id INT,
    XMLValue XML
);

INSERT @tempTable
(
    id,
    XMLValue
)
SELECT id,
       CAST('<monthdate>' + value AS XML)
FROM @sample
    CROSS APPLY STRING_SPLIT(REPLACE(xmlSnippet, '<monthdate>', '&'), '&')
WHERE value <> '';
SELECT *
FROM @tempTable;

DECLARE @tempTable2 TABLE
(
    monthDate DATE,
    autoItemNo INT,
    amount MONEY,
    currentAmount MONEY
);

WITH myData (id, xmlRow)
AS (SELECT id,
           CAST('<monthdate>' + value AS XML)
    FROM @sample
        CROSS APPLY STRING_SPLIT(REPLACE(xmlSnippet, '<monthdate>', '&'), '&')
    WHERE value <> '')
INSERT @tempTable2
(
    monthDate,
    autoItemNo,
    amount,
    currentAmount
)
SELECT myData.xmlRow.value('/monthdate[1]', 'date'),
       myData.xmlRow.value('/automitemno[1]', 'int'),
       myData.xmlRow.value('/amount[1]', 'money'),
       myData.xmlRow.value('/currentamount[1]', 'money')
FROM myData;

SELECT *
FROM @tempTable2;
DECLARE@sample表
(
id INT标识,
xmlSnippet VARCHAR(最大值)
);
插入@sample
(
xmlSnippet
)
价值观
('2019-07-01

这里有一个解决方案,它可以提供与所写内容完全相同的文本:

declare @xml as nvarchar(max)

set @xml = N'<monthdate>2019-07-01</monthdate>
<automitemno>302</automitemno>
<amount>1.190000000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>
<monthdate>2019-07-01</monthdate>
<automitemno>2131</automitemno>
<amount>0.386750000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>'


select replace(substring(a.value, charindex('<monthdate>',a.value)+11, charindex('</amount>', a.value)-16), char(10), '') as [XMLValue]

from string_split(replace(@xml, '</langitemno>', char(30)), char(30)) a

where a.value like '%<monthdate>%</amount>%'
将@xml声明为nvarchar(max)
set@xml=N'2019-07-01

您真的希望输出没有符号(
<&>
)?还是这只是问题格式的问题?您好,是的,是问题格式,抱歉。我想要()在输出中,因为我需要在仪表板报表的其他几个查询中使用temp表。另外,您使用的是哪个SQL引擎?MS SQL Server、MySQL、PostgreSQL,以及哪个版本?MS SQL Server Management studio 17感谢上述内容,v很有帮助,只是需要进一步澄清。我如何从表和列中调用它,即与其将该值设置为已知的Nvarchar值,不如指定该值所在的表和列,并将结果插入临时表中?实际上,我是从表中调用它,@sample是一个表变量,可能是一个真正的持久表。根据临时表,只需替换表变量(@tentable)使用类似于#tentable.hey:I query返回的临时表:无效的对象名称“STRING_SPLIT”?有什么想法吗?它可以识别命令,因为它是粉红色的,但出现了错误?STRING_SPLIT是在2016版(兼容级别130)中引入的。您使用的是什么版本?检查我提供的链接的db fiddle demo。
DECLARE @sample TABLE
(
    id INT IDENTITY,
    xmlSnippet VARCHAR(MAX)
);

INSERT @sample
(
    xmlSnippet
)
VALUES
('<monthdate>2019-07-01</monthdate>
<automitemno>302</automitemno>
<amount>1.190000000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>
<monthdate>2019-07-01</monthdate>
<automitemno>2131</automitemno>
<amount>0.386750000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>');


DECLARE @tempTable TABLE
(
    id INT,
    XMLValue XML
);

INSERT @tempTable
(
    id,
    XMLValue
)
SELECT id,
       CAST('<monthdate>' + value AS XML)
FROM @sample
    CROSS APPLY STRING_SPLIT(REPLACE(xmlSnippet, '<monthdate>', '&'), '&')
WHERE value <> '';
SELECT *
FROM @tempTable;

DECLARE @tempTable2 TABLE
(
    monthDate DATE,
    autoItemNo INT,
    amount MONEY,
    currentAmount MONEY
);

WITH myData (id, xmlRow)
AS (SELECT id,
           CAST('<monthdate>' + value AS XML)
    FROM @sample
        CROSS APPLY STRING_SPLIT(REPLACE(xmlSnippet, '<monthdate>', '&'), '&')
    WHERE value <> '')
INSERT @tempTable2
(
    monthDate,
    autoItemNo,
    amount,
    currentAmount
)
SELECT myData.xmlRow.value('/monthdate[1]', 'date'),
       myData.xmlRow.value('/automitemno[1]', 'int'),
       myData.xmlRow.value('/amount[1]', 'money'),
       myData.xmlRow.value('/currentamount[1]', 'money')
FROM myData;

SELECT *
FROM @tempTable2;
declare @xml as nvarchar(max)

set @xml = N'<monthdate>2019-07-01</monthdate>
<automitemno>302</automitemno>
<amount>1.190000000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>
<monthdate>2019-07-01</monthdate>
<automitemno>2131</automitemno>
<amount>0.386750000000</amount>
<currentamount>0.000000000000</currentamount>
<langitemno>1</langitemno>'


select replace(substring(a.value, charindex('<monthdate>',a.value)+11, charindex('</amount>', a.value)-16), char(10), '') as [XMLValue]

from string_split(replace(@xml, '</langitemno>', char(30)), char(30)) a

where a.value like '%<monthdate>%</amount>%'