问题
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