在SQL中读取XML布尔值

时间:2021-07-25 16:58:04

How to read Boolean values from XML Column in SQL. Below is sample code. Is it achievable using XQuery Value function.

如何在SQL中从XML列读取布尔值。下面是示例代码。是否可以使用XQuery值函数实现。

 DECLARE @XML XML = '<form>
                     <field name="BAR_prior_report" type="Boolean"><value>false</value></field>
                      <field name="BAR_multiple_branches" type="Boolean"><value>true</value></field>
                  </form>'

i would like to see BAR_prior_report as False and BAR_multiple_branches as True

我希望看到BAR_prior_report为False,而bar_multiple_branch为True

1 个解决方案

#1


5  

This will return the fields as BIT columns, which most languages will parse as a boolean (there is no boolean in SqlServer).

这将返回作为位列的字段,大多数语言将把这些字段解析为布尔值(SqlServer中没有布尔值)。

SELECT
  Nodes.node.value('(field[@name="BAR_prior_report"]/value)[1]', 'bit') 
    AS BAR_prior_report,
  Nodes.node.value('(field[@name="BAR_multiple_branches"]/value)[1]', 'bit') 
    AS BAR_multiple_branches
FROM
  @XML.nodes('//form') AS Nodes(node);

Fiddle here

小提琴在这里

You can always pass this through another projection for further processing, e.g. here

你可以通过另一个投影进行进一步的处理,例如这里

#1


5  

This will return the fields as BIT columns, which most languages will parse as a boolean (there is no boolean in SqlServer).

这将返回作为位列的字段,大多数语言将把这些字段解析为布尔值(SqlServer中没有布尔值)。

SELECT
  Nodes.node.value('(field[@name="BAR_prior_report"]/value)[1]', 'bit') 
    AS BAR_prior_report,
  Nodes.node.value('(field[@name="BAR_multiple_branches"]/value)[1]', 'bit') 
    AS BAR_multiple_branches
FROM
  @XML.nodes('//form') AS Nodes(node);

Fiddle here

小提琴在这里

You can always pass this through another projection for further processing, e.g. here

你可以通过另一个投影进行进一步的处理,例如这里