Oracle extract from xml truncating value

前端 未结 1 1040
梦如初夏
梦如初夏 2021-01-28 04:57

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 g

相关标签:
1条回答
  • 2021-01-28 05:38

    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
    
    0 讨论(0)
提交回复
热议问题