Check length of column in XMLTable

梦想与她 提交于 2019-12-24 13:26:42

问题


This is my XML in an XMLTYPE column:

<customers>
<customer>
  <name>abc</name>
  <surname>abc</surname>
  <address>abc</address>
</customer>
<customer>
   <name>abc</name>
  <surname>abc</surname>
  <address>abc</address>
</customer>
</customers>

I want that if the length of a field exceeds the allowed length, 15 charcaters, then use a default value instead.

In the guide for DB2 I found this example:

select ...from .., XMLTABLE 
('$INFO/customerinfo*' passing 
columns 
city varchar(16) path a'addr/city(if(string-lenght(.)<=16) then . else "Error!")' 
) ..

How can I do it in Oracle? This is my current XMLTABLE attempt:

select ..XMLTABLE 
('customers/*' passing ..
columns 
...
"address"  varchar(15) path 'indirizzo/(if(lenght(.)<=15) then . else "Error!")' 
) data;

But that gets:

ORA-19237. unable to resolve call to function -fn:length


回答1:


You have typos and inconsistent naming so it's hard to tell exactly what you're really doing to get that error, but it works if you use the string-length() function from your DB2 example, rather than length() (or lenght()):

"address" varchar2(15) path 'address/(if(string-length(.)<=15) then . else "Error!")' 

With modified data to trigger the error behaviour:

select *
from XMLTABLE ('customers/*'
 passing xmltype('<customers>
 <customer>
  <name>abc</name>
  <surname>abc</surname>
  <address>abc def ghi jkl mno</address>
 </customer>
 <customer>
   <name>abc</name>
  <surname>abc</surname>
  <address>abc</address>
 </customer>
</customers>')
columns 
  "address"  varchar(15) path 'address/(if(string-length(.)<=15) then . else "Error!")' 
) data;

address       
---------------
Error!         
abc            


来源:https://stackoverflow.com/questions/36050202/check-length-of-column-in-xmltable

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