是否可以运行带有XPath“ where”子句的SQL查询,并在比较之前修剪尾随空格?
我有一个SQL XML列,其中有XML节点,这些节点的属性包含尾随空格。我想找到一个给定的记录,它具有指定的属性值-没有尾随空格。
当我尝试时,我得到...
“没有函数'{http://www.w3.org/2004/07/xpath-functions}:normalize-space()'”
我尝试了以下操作(查询1有效,查询2无效)。这是在SQL 2005上。
declare @test table (data xml)
insert into @test values ('<thing xmlns="http://my.org.uk/Things" x="hello " />')
-- query 1
;with xmlnamespaces ('http://my.org.uk/Things' as ns0)
select * from @test where data.exist('ns0:thing[@x="hello "]') != 0
-- query 2
;with xmlnamespaces ('http://my.org.uk/Things' as ns0)
select * from @test where data.exist('ns0:thing[normalize-space(@x)="hello"]') != 0
感谢您的帮助。
SQL Server也没有包含允许正则表达式的replace()
函数。最好的选择是使用contains()
,它应该适合您的搜索(不使用通配符时,嵌套子字符串问题将不存在)。
select * from @test where data.exist('ns0:thing[contains(@x, "hello")]') != 0
如果是SQL Server 2005/2008,则为SQLXML 4.0,不支持此功能(normalize
)。
请参阅this MSDN文章以了解更多详细信息。
我建议将XML转换为字符串,并改用RTRIM
,LTRIM
和SQL的其他字符串函数。