如何从 XML 内容中拆分字符串并获取所需的值
How to split string from XML content and get the required value
大家好,我正在转换一个 xml 内容并将其插入到一个 table 变量中,如下所示
DECLARE @iDoc int
SET @XMLData = '<NewDataSet>
<Table>
<DataId>2324205.3933251.7336404</DataId>
<IsVisible>true</IsVisible>
<Notes />
<Marks>85.5</Marks>
</Table>
</NewDataSet>'
EXEC sp_xml_preparedocument @idoc OUTPUT, @XMLData
SELECT DataId FROM OPENXML(@idoc, 'NewDataSet/Table', 1)
WITH (DataId NVARCHAR(250) 'DataId')```
I would like to split the dot value and retrieve the the first value, can some one help me how to can I do that with in XML
IsVisible is a bit filed, Marks is deicmal like that I will have
从SQLServer 2005开始,在处理XML数据类型时,最好使用基于w3c标准的XQuery语言。
保留 Microsoft 专有 OPENXML
及其同伴 sp_xml_preparedocument
和 sp_xml_removedocument
只是为了与过时的 SQL 向后兼容
Server 2000。它们的使用减少到极少数边缘情况。
强烈建议重新编写 SQL 并将其切换为 XQuery。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, Notes NVARCHAR(MAX));
INSERT INTO @tbl (Notes) VALUES
(N'<NewDataSet>
<Table>
<DataId>2324205.3933251.7336404</DataId>
</Table>
</NewDataSet>');
-- DDL and sample data population, end
WITH rs AS
(
SELECT *
, TRY_CAST(Notes AS XML).value('(/NewDataSet/Table/DataId/text())[1]', 'VARCHAR(MAX)') AS x
FROM @tbl
)
SELECT *
, LEFT(x, CHARINDEX('.', x) - 1) AS [After]
, PARSENAME(x, 3) AS [After2]
FROM rs;
输出
+-------------------------+---------+
| Before | After |
+-------------------------+---------+
| 2324205.3933251.7336404 | 2324205 |
+-------------------------+---------+
大家好,我正在转换一个 xml 内容并将其插入到一个 table 变量中,如下所示
DECLARE @iDoc int
SET @XMLData = '<NewDataSet>
<Table>
<DataId>2324205.3933251.7336404</DataId>
<IsVisible>true</IsVisible>
<Notes />
<Marks>85.5</Marks>
</Table>
</NewDataSet>'
EXEC sp_xml_preparedocument @idoc OUTPUT, @XMLData
SELECT DataId FROM OPENXML(@idoc, 'NewDataSet/Table', 1)
WITH (DataId NVARCHAR(250) 'DataId')```
I would like to split the dot value and retrieve the the first value, can some one help me how to can I do that with in XML
IsVisible is a bit filed, Marks is deicmal like that I will have
从SQLServer 2005开始,在处理XML数据类型时,最好使用基于w3c标准的XQuery语言。
保留 Microsoft 专有 OPENXML
及其同伴 sp_xml_preparedocument
和 sp_xml_removedocument
只是为了与过时的 SQL 向后兼容
Server 2000。它们的使用减少到极少数边缘情况。
强烈建议重新编写 SQL 并将其切换为 XQuery。
SQL
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, Notes NVARCHAR(MAX));
INSERT INTO @tbl (Notes) VALUES
(N'<NewDataSet>
<Table>
<DataId>2324205.3933251.7336404</DataId>
</Table>
</NewDataSet>');
-- DDL and sample data population, end
WITH rs AS
(
SELECT *
, TRY_CAST(Notes AS XML).value('(/NewDataSet/Table/DataId/text())[1]', 'VARCHAR(MAX)') AS x
FROM @tbl
)
SELECT *
, LEFT(x, CHARINDEX('.', x) - 1) AS [After]
, PARSENAME(x, 3) AS [After2]
FROM rs;
输出
+-------------------------+---------+
| Before | After |
+-------------------------+---------+
| 2324205.3933251.7336404 | 2324205 |
+-------------------------+---------+