【问题标题】:How to select xml node value from a table column in SQL如何从 SQL 中的表列中选择 xml 节点值
【发布时间】:2021-08-05 08:43:36
【问题描述】:

我有一个文本数据类型的 sql 表列,其中包含 xml 值。当我尝试选择时,它给出了一个空白值。有人可以帮我吗?

表结构

Sid (int) | xmlresults (text) | recivedDate (Datetime)

用于检索的查询

;WITH XMLNAMESPACES (N'http://www.w3.org/2001/XMLSchema' AS NS)
SELECT
X.S.value('(NS:examinationDate)[1]', 'VARCHAR(MAX)') as xmlvalue
FROM examinationresults er
CROSS APPLY (SELECT CAST(xmlresults AS XML)) AS [XML](xmlresults)
CROSS APPLY [XML].xmlresults.nodes('/NS:NewExamResults/NS:Table1') AS X(S)
where  er.sid='8596123'

XML 数据

<NewExamResultsType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <NewExamResults xmlns="urn:ex.likeexam.com">
    <Table>
      <rowCount>1</rowCount>
    </Table>
    <Table1>
      <StudId>9447524123</StudId>
      <examinationDate>26/10/2019</examinationDate>
      <VerificationDate>111111111</VerificationDate>
      <examiner>Williams</examiner>
      <ExamResults>
        <ExamResult>
          <ExamCode>110</ExamCode>
          <StudName>Tony K</StudName>
          <StudCode>K</StudCode>
        </ExamResult>
      </ExamResults>
    </Table1>
  </NewExamResults>
</NewExamResultsType>

【问题讨论】:

    标签: sql-server xml tsql


    【解决方案1】:

    您的示例文档中似乎没有引用命名空间 URI http://www.w3.org/2001/XMLSchemahttp://www.w3.org/2001/XMLSchema-instance。重要的命名空间 URI 是 urn:ex.likeexam.comNewExamResults 节点及其所有子节点都驻留在其中。请尝试以下查询...

    with xmlnamespaces (
      N'urn:ex.likeexam.com' as le
    )
    select convert(date, table1.value(N'(le:examinationDate/text())[1]', N'varchar(10)'), 103) as examinationDate
    from examinationresults
    cross apply (select cast(xmlresults as xml)) c1(xmlValue)
    cross apply xmlValue.nodes(N'/NewExamResultsType/le:NewExamResults/le:Table1') c2(Table1)
    where sid='8596123';
    

    【讨论】:

    • 它现在正在工作。需要包含日期时间而不是日期。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多