2011-02-15 29 views
4

值的字段如下XQuery的LIKE操作符(開始 - 含)

<en-US>Parameter23</en-US> 
<it-IT>Parameter</it-IT> 

SQL查詢

select * 
from parametermaster 
where cast(ParameterName as xml).exist('en-US/text()[contains(.,"P")]') = 1 

,我試圖把它作爲

select * 
from parametermaster 
where cast(ParameterName as xml).exist('en-US/text()[starts-with(.,"P")]') = 1 

錯誤是

Msg 2395,Level 16,State 1,Line 1
XQuery [exist()]:沒有函數 '{http://www.w3.org/2004/07/xpath-functions}:starts-與()'

任何人都可以幫助我,我想在SQL 2005 XQuery中創建LIKE操作符感覺。我是XQuery的新手。

回答

4

如何:

select * 
from parametermaster 
where cast(ParameterName as xml).value("(en-US)[1]", "varchar(50)") LIKE 'P%' 

基本上是:

  • en-US XML元素和它的值轉換爲varchar(50)
  • 然後做有規律的,正常的SQL LIKEvarchar(50)
+0

由於它的工作,但在** XML鑄造它**然後再在** ** VARCHAR這是不好的,我認爲你能告訴我任何方式不重複鑄造在這 – 2011-02-15 06:50:51

10

點開始-用()/結束-用()可以通過串()和字符串長度的組合來取代的()函數:

select * 
from parametermaster 
where cast(ParameterName as xml).exist('en-US/text()[substring(., 1, string-length("P")) = "P"]') = 1 

通常開始-與(A,B)等同於

substring(a, 1, string-length(b)) = b 

結束,用(A,b)相當於

substring(a, string-length(a) - string-length(b)) = b