Unable to Extract XML value from Oracle CBLOB - sql

I am trying to select a specific xml value as column in a Oracle 11G table which is stored as XML - Huge CLOB, but unable to. Kindly help
Contents of XML as Below
<Bid xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/LCC.Crew.FAReserves.wsvc.Entities.FAReserves">
<AggressiveBidType></AggressiveBidType>
<BidCriteria>
<BidCriteria i:type="RapBidCriteria">
<Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">BAC</Value>
</BidCriteria>
</BidCriteria>
<BidItem>RAP</BidItem>
<BidName>BAC</BidName>
<BidType>Standing</BidType>
<CatsId>10023</CatsId>
<EmployeeBidId>10620</EmployeeBidId>
<EmployeeId>135289</EmployeeId>
<EndDate>2015-03-29T00:00:00Z</EndDate>
<IsAggressive>false</IsAggressive>
<IsLodo>false</IsLodo>
<IsOnPremiseReserve>false</IsOnPremiseReserve>
<OperatingDate>2015-02-25T00:00:00Z</OperatingDate>
<Priority>0</Priority>
</Bid>
Below Statement return null
SELECT extract(XMLTYPE(XMLBIDCONTENT),'/Bid/BidName/text()') "REFERENCE"
FROM AOCREWBIDDING.EMPLOYEEBIDS
Where EmployeeBidID = 100
Below statement returns error
ORA-00932: inconsistent datatypes: expected - got -
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action: Error at Line: 83 Column: 8
SELECT extract(XMLBIDCONTENT,'/Bid/BidName/text()').getStringVal() "REFERENCE"
FROM AOCREWBIDDING.EMPLOYEEBIDS
Where EmployeeBidID = 100

The extract() function is deprecated. It's better to use XMLQuery().
You need to either declare a default namespace to match the one in the XML document:
select XMLQuery('
declare default element namespace
"http://schemas.datacontract.org/2004/07/LCC.Crew.FAReserves.wsvc.Entities.FAReserves"; (: :)
/Bid/BidName/text()'
passing XMLType(xmlbidcontent)
returning content) as BidName
from employeebids
where EmployeeBidID = 100;
BIDNAME
--------------------------------------------------------------------------------
BAC
or (simpler but less robust) use a wildcard:
select XMLQuery('/*:Bid/*:BidName/text()'
passing XMLType(xmlbidcontent)
returning content) as BidName
from employeebids
where EmployeeBidID = 100;
BIDNAME
--------------------------------------------------------------------------------
BAC
db<>fiddle showing your original queries and both of these, using a CTE to provide the sample CLOB value.

Related

How to use XMLTABLE (Oracle SQL) in combination with attribute names

I'm querying an Oracle SQL table containing XML.
The (simplified) XML structure is as follows:
<aggregate type="HeadAggregate">
<entity type="Default" root="true" id="asdb7e9e-93324-43242d-b83a-f2d3202ed">
<attribute name="ObjectName" multivalue="false">ExampleName</attribute>
<attribute name="Subprocesses" multivalue="false">false</attribute>
<attribute name="ObjectDesc" multivalue="false">Description</attribute>
</entity>
<aggregate>
I want to get to retreive the object name. Therefore I wrote the following query:
SELECT xt.*
FROM DATABASENAME.TABLENAME x,
XMLTABLE('/aggregate/entity[#type = ''Default'']'
PASSING XMLTYPE(x.DATA)
COLUMNS
attribute_name VARCHAR2(100) PATH 'attribute[1]'
) xt
So far so good. This works fine! I get the desired output.
However, now I want to replace '[1]' in PATH by a reference to the attribute name to make my script a bit more flexible.
So I changed my script to:
SELECT xt.*
FROM DATABASENAME.TABLENAME x,
XMLTABLE('/aggregate/entity[#type = ''Default'']'
PASSING XMLTYPE(x.DATA)
COLUMNS
attribute_name VARCHAR2(100) PATH 'attribute[#name = ''ObjectName'']'
) xt
For some reason, now I get the following error message:
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 3 Column: 47
I've been struggling on this for a while.
What am I doing wrong?
Many thanks in advance!
You can (or should) use double-quotes, rather than escaped single-quotes:
SELECT xt.*
FROM DATABASENAME.TABLENAME x,
XMLTABLE('/aggregate/entity[#type = "Default"]'
PASSING XMLTYPE(x.DATA)
COLUMNS
attribute_name VARCHAR2(100) PATH 'attribute[#name = "ObjectName"]'
) xt
db<>fiddle

How to extract data (via SQL query) from XML stored in a column as type BLOB

I need help with the following.
I have a table which has one of the columns as type BLOB. Executing following query allows me to read the BLOB in the Oracle SQL developor's text editor as the image below:
select
utl_raw.cast_to_varchar2(utl_compress.lz_uncompress(a.DATA))
from my_table a where a.id = 11266392;
I can double click on above shaded cell and read it in text. This is nice but it only works for one row at a time. My actual goal is to read specific data within xml tags from each of these BLOBs and there are roughly a million of those for each month.
I thought i could cast this into a string and read the first 4000 characters but that was useless as my BLOBs for each record are of length 400K (using getlength procedure from dbms_lob).
I tried casting the blob in XML via this
select CONVERT(xml,a.data, 2) from
(select utl_compress.lz_uncompress(a.DATA) as data from my_table a where a.id = 11266392) a;
But this threw an error:
ORA-00904: "XML": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 10 Column: 16
My question is then, is there a way I can use extract XML, XQuery or XMLTable on the blob itself (after I have applied the lz_uncompress function on it)?
What are my other alternatives?
Here is the heavily edited XML in one of the blob (note that I have about 10-15 such Worksheet tags)
<Worksheets>
<Worksheet Description="Some Coverage" EffectiveDate="2020-06-28T00:01:00-05:00" ExpirationDate="2021-06-28T00:01:00-05:00" FixedId="Table:13263928">
<Routine RateBookCode="XX" RateBookEdition="00006" RoutineCode="XXX" RoutineVersion="1">
<Store Declaration="true" Result="false" ResultType="java.lang.Boolean" Variable="hasSpecialLimits">
<PropertyGet ObjectName="XXX" ObjectType="XXX" ObjectValue="My Address" PropertyName="HasSpecialLimits" Type="XXXXX" Value="false" ValueType="java.lang.Boolean"/>
</Store>
</Routine>
</Worksheet>
</Worksheets>
It should work like this:
SELECT XMLTYPE( a.data, 2 )
FROM ( select utl_compress.lz_uncompress(a.DATA) as data
from my_table a where a.id = 11266392 ) a;

AWS Athena: Error parsing field value and unexpected query results

I have the following table schema prepared by AWS glue
When I query the table using SELECT * FROM "vietnam-property-develop"."sell" limit 10;, it throws an error:
HIVE_BAD_DATA: Error parsing field value '{"area":"85
m²","date":"14/01/2020","datetime":"2020-01-18
00:42:28.488576+00:00","address":"Quan Hoa - Cầu Giấy","price":"20
Tỷ","cat":"Bán nhà mặt
phố","lon":"105.7976502","avatar":"","id":"24169794","title":"Chính
chủ cần bán nhà mặt phố nguyễn văn huyên Quan Hoa Cầu Giấy, 2 tầng, dt
85m2. LH 0903233723","lat":"21.0376771","room":"0"}' for field 4:
org.openx.data.jsonserde.json.JSONObject cannot be cast to
java.lang.Double
Then I tired to just query the title column by using SELECT title FROM "vietnam-property-develop"."sell" limit 10;
It returns result which I didn't expect. It seems that the query return the whole json files instead of just the title column. And the number of rows is 4 but not 10 no matter how I modify the query.

Facing issue while running select query on DB2

I'm querying a DB2 table (STG_TOOL) with 2 columns - T_L_ID - Integer, Name - VARCHAR(20).
SELECT T_L_ID, Name FROM STG_TOOL;
The query returns answer. However, the below query gives error.
SELECT T_L_ID, RTRIM(Name) FROM STG_TOOL;
This query gives error at 78th row.
DB2 Database Error: ERROR [42815] [IBM][DB2] SQL0171N The data type,
length or value of the argument for the parameter in position "1" of
routine "SYSIBM.RTRIM" is incorrect. Parameter name: "". 1 0
The reason identified is that Name in 78th row has a replacement character - '�'.
Even, the same query with a where clause gives us the error.
SELECT T_L_ID, RTRIM(Name) FROM STG_TOOL WHERE T_L_ID = 78;
The sample date on 78th rows is T_L_ID = 1040 & Name = 'test�'
The above mentioned error re-occurs for the above query.
What does the error implies? How can this be handled/solved?
Adding details to the post:
Version: DSN11010 (version 11)
OS: z/OS
Encoding: Unicode
Toad for DB2 is being used for querying. Toad version - 5.5

Oracle CONTAINS Giving Invalid Identifier ORA-00904

So I have a webapp that takes in a string where ID elements are broken up by '-' (e.g. someid1-someid2). I then convert the string to something like someid1 OR someid2. This in theory according to Oracle's doc's should allow me to then do something like
SELECT somecol1, somecol2 FROM sometable WHERE CONTAINS (somecol1, 'someid1 OR someid2') > 0;
However I'm getting the following error when running the sample query in SQL Dev:
ORA-00904: "CONTAINS": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
The version of Oracle is Oracle9i Enterprise Edition Release 9.2.0.5.0 - Production of which the documentation is for, so I don't see why CONTAINS would be considered an invalid identifier. Adding the other optional parameter
SELECT somecol1, somecol2 FROM sometable WHERE CONTAINS (somecol1, 'someid1 OR someid2', 1) > 0;
also gives the same error. What am I/it doing wrong here?
EDIT:
'Working Code' as per request although arguably it isn't working
SELECT tech, product FROM tech_det_view WHERE CONTAINS (tech, 'test OR qual') > 0;
You should create oracle text index before using CONTAINS function
CREATE INDEX idxName ON tableName(columnName) INDEXTYPE IS CTXSYS.CONTEXT;
Here you can find more information about Oracle Text.