Question

We have a CLOB column where we're storing fairly simple XML, from which I need to extract the property of one of the tags. I believe the select statement is correct, since I get a portion of the value, but it's truncating it at 80 characters.

Here's a snippet of the XML...

<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <customer>
         <customer_id>ABCDE12346</customer_id>
         <first_name>FAKE</first_name>
         <last_name>CUSTOMER</last_name>
      </customer>
...
    <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here" thumbnail="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here_thumb"/>
</images>
...

...and here's a snippet of the SQL...

select
    xmltype(contact_data).extract('/service_order/@order_number').getStringVal ordnum
   ,extract(xmltype(contact_data) ,'//images/image/@src') imgsrc
from
    my_table
where
    trunc(contact_date) = trunc(sysdate)
and extractvalue(xmltype(contact_data) ,'/service_order/@order_number') = '&ordnum'

The beginning of the URL returns but, as previously stated, the URL is truncated at 80 characters.

What am I doing wrong?

Was it helpful?

Solution

Are you executing this in sqlplus? Extract returns a XMLType instance, and is displayed according to long variable, which is defaulted to 80. If you increase the value you can see the full URL.

SQL> with x(contact_data) as (
select '<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here"/>
        </images>
</service_order>
</service_orders>' from dual
)
select
    length(extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src')) as url_length,
    extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src') as url
from
    x;  

URL_LENGTH
----------
URL
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       101
https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_b

After increasing the value for long variable,

SQL> set long 120

SQL> with x(contact_data) as (
select '<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here"/>
        </images>
</service_order>
</service_orders>' from dual
)
select
    length(extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src')) as url_length,
    extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src') as url
from
    x;  

URL_LENGTH
----------
URL
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       101
https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here

You can use the getStringVal function to convert the XMLType to varchar2, which doesn't depend on long variable.

SQL> set long 80

SQL> with x(contact_data) as (
select '<?xml version="1.0" encoding="UTF-8"?>
<service_orders count="1">
   <service_order order_number="fakefakefake" id="fakefakefake">
      <images count="2">
       <image src="https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here"/>
        </images>
</service_order>
</service_orders>' from dual
)
select
    length(extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src')) as url_length,
    extract(xmltype(contact_data),'/service_orders/service_order/images/image/@src').getStringval() as url
from
    x; 

URL_LENGTH
----------
URL
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
       101
https://s3.amazonaws.com/some_subfolder/deeper/deeper_still/ever_deeper/really_big_long_url_goes_here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top