how to perform a case-insensitive attribute selector in xquery

梦想的初衷 提交于 2019-12-23 20:06:57

问题


I'm trying to query an XML column in SQL server, and I want to select records where an attribute value matches a case-insensitive string.

For instance, consider the following value for the XML column:

<items>
  <item k="Description" v="hello" />
</items>

Right now, my select looks like this:

SELECT
MyXmlColumn.value('(/items/item[@k="Description"]/@v)[1]', 'nvarchar(max)') as v
FROM Table

The problem is that the value of the "k" attribute could be 'Description', 'description', or 'DESCRIPTION'.

How do I write the XQuery so that it performs a case-insensitive match?


回答1:


You can use the lower-case function fn:lower-case (which will make the attributes you are searching on lower-case) and then make sure that in your query you use lower-case:

SELECT 
MyXmlColumn.value('(/items/item[fn:lower-case(@k)="description"]/@v)[1]', 'nvarchar(max)') as v
FROM [Table]


来源:https://stackoverflow.com/questions/11335558/how-to-perform-a-case-insensitive-attribute-selector-in-xquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!