Extract data from XML Clob using SQL from db2 - sql

I want to extract the value of Decision using sql from table TRAPTABCLOB having column testclob with XML stored as clob. IN DB2
Sample XML as below
<?xml version="1.0" encoding="UTF-8"?>
<DCResponse>
<Status>Success</Status>
<Authentication>
<Status>Success</Status>
</Authentication>
<ResponseInfo>
<ApplicationId>5701200</ApplicationId>
<SolutionSetInstanceId>
63a5c214-b5b5-4c45-9f1e-b839a0409c24
</SolutionSetInstanceId>
<CurrentQueue />
</ResponseInfo>
<ContextData>
<!--Decision Details Start-->
<Field key="SoftDecision">A</Field>
<Field key="**Decision**">1</Field>
<Field key="NodeNo">402</Field>
<Field key="NodeDescription" />
<!--Decision Details End-->
<!--Error Details Start-->
<Field key="ErrorResponse">
<Response>
<Status>[STATUS]</Status>
<ErrorCode>[ERRORCODE]</ErrorCode>
<ErrorDescription>[ERRORDESCRIPTION]</ErrorDescription>
<Segment>[SEGMENT]</Segment>
</Response>
</Field>
<Field key="ErrorCode">0</Field>
<Field key="ErrorDescription" />
</ContextData>
</DCResponse>

One of the nice things about using XMLTABLE() is that it produces an expression that can be used as a subquery or joined to a table or another SQL expression.
SELECT x.decision
FROM traptabclob, XMLTABLE(
'$d/DCResponse/ContextData[1]' PASSING XMLPARSE(DOCUMENT testclob) AS "d"
COLUMNS
DECISION CHAR(1) PATH 'Field[#key="**Decision**"][1]'
) AS x
;

Related

Extract Date Value from child node

I am needing to extract a date from a nested xml and am running into the issue of no output. The date I am attempting to retrieve is for Field name="A". My expected output is 2022-04-12. Here is the sample data along with the query I have generated. Not exactly sure where I am going wrong with the query. Any help will be greatly appreciated:
declare #xml xml
set #xml =
'<root xlmns="http://www.example.com">
<header></header>
<alt-header></alt-header>
<Main>
<Employee>
<Department>
<Category>
<Team>
<Name>Fun</Name>
<Sub>
<field name="A">
<Field-value xmlns:xsi="http://www.example2.com" xsi:type="date">
<value>2022-04-12</value>
</Field-value>
</field>
<field name="B">
<Field-value xmlns:xsi="http://www.example2.com" xsi:type="date">
<value>2021-04-12</value>
</Field-value>
</field>
</Sub>
</Team>
</Category>
</Department>
</Employee>
<Employee>
<Department>
<Category>
<Team>
<Name>Times</Name>
<Sub>
<field name="B">
<Field-value xmlns:xsi="http://www.cde.com" xsi:type="date">
<value>2021-04-12</value>
</Field-value>
</field>
</Sub>
</Team>
</Category>
</Department>
</Employee>
</Main>
</root>';
WITH XMLNAMESPACES (DEFAULT 'http://www.example.com',
'http://www.example2.com' as xsi)
SELECT x.query('./Department/Category/Team/Name[../Name="FUN"]/Sub/Field [#name="A"]/field-value/value').value('.', 'nvarchar(100)')A_date
from #xml.nodes('/root/Main/Employee') tempxml(x)
//Department/Category/Team[Name[text()="Fun"]]/Sub/field[#name="A"]/Field-value/value
The // in the beginning means that xpath will search everythere in the document. Since you are looking within Employee elements, you can change //Department to ./Department or even Department. All three should work.

I want to index multivalue dateRange in solr

I want to index daterange as multifiled in solr
eg----- temp_close_timing=[2022-02-08T16:40:49 TO 2022-02-28T16:40:49,2022-02-08T16:40:49 TO 2022-10-28T16:40:49,2022-02-08T16:40:49 TO 2022-02-28T16:40:49,2022-02-17T00:00:00 TO 2022-02-28T23:00:00]
can someone help me in this.
I am doing as shown below but getting error in this
My scehma file show below:
<field name="temp_close_timing" type="rdate" stored="true" omitNorms="true"
multiValued="true"/>
my dataconfig FIle:
<!-- temp closing timing of restaurant -->
<entity name="tempRestaurant" dataSource="dineout" onError="skip"
query=" SELECT
GROUP_CONCAT(CONCAT(DATE_FORMAT(rtc.start_dt, '%Y-%m-%dT%T'),
'.0Z',
' TO ',
DATE_FORMAT(rtc.end_dt, '%Y-%m-%dT%T'),
'.0Z')) AS temp_close_timing,
rtc.rest_id AS restaurant_id
FROM
restaurant_temp_close AS rtc
WHERE
rtc.status = 1"
transformer="RegexTransformer"
cacheKey="restaurant_id" cacheLookup="rest.r_id" cacheImpl="SortedMapBackedCache">
<field name="temp_close_timing" column="temp_close_timing"/>
</entity>
I got the solution of this question
<!-- temp closing timing of restaurant -->
<entity name="tempRestaurant" dataSource="dineout" onError="skip"
query=" SELECT
GROUP_CONCAT(CONCAT('[',
DATE_FORMAT(tr.start_dt, '%Y-%m-%dT%T'),
' TO ',
DATE_FORMAT(tr.end_dt, '%Y-%m-%dT%T'),
']')
SEPARATOR '#####') AS temp_close_timing,
tr.rest_id AS restaurant_id
FROM
restaurant_temp_close AS tr
WHERE
tr.status = 1
GROUP BY tr.rest_id"
transformer="RegexTransformer"
cacheKey="restaurant_id" cacheLookup="rest.r_id" cacheImpl="SortedMapBackedCache">
<field name="temp_close_timing" column="temp_close_timing" splitBy="#####"/>
</entity>
can anybody tell how to apply intersect of contain in this field with exampl ..I want to check that todays date/time lies in this range or not

Read an XML stored in Oracle DB in CLOB Data type [duplicate]

This question already has answers here:
Extracting XML sub-tags from a clob in Oracle via SQL
(3 answers)
Closed 1 year ago.
I'm trying to read the values from the XML format stored in clob data type in Oracle DB.
from below XML stored in CLOB column i wanted to read the specific values like "entityFldType="uid""
can anyone help me with SQL
"d258b8c7-fa47-49d4-a28c-f779d39" xmlns:even="http://www.cos.com/EDA/Event100">
<Body userId="administrator" entityType="USER" internalId="uid=usertest1,ou=People,dc=abcde,dc=medical,dc=com" datasource="LAP">
<Field entityFldType="uid">
<value>usertest1</value>
</Field>
<Field entityFldType="userPassword">
<value>********</value>
</Field>
<Field entityFldType="telephoneNumber">
<value>000 00000/55555</value>
</Field>
<Field entityFldType="employeeType">
<value>active</value>
</Field>
<Field entityFldType="mail">
<value>usertest1#coz.com</value>
</Field>
<Field entityFldType="initials">
<value>a</value>
</Field>
<Field entityFldType="objectClass">
<value>top</value>
</Field>
<Field entityFldType="sn">
<value>qa</value>
</Field>
<Field entityFldType="cn">
<value>qa</value>
</Field>
</Body>
</even:Event>
Your XML got a little mangled, but an easy way is to convert to XMLTYPE which opens up many of the XMLDB features. So you'll have something along the lines:
select
extractvalue(my_xml, '/body/field/text()') tag_value,
extractvalue(my_xml, '/body/field/#entityFldType') attr_value
from ( select xmltype(clob) my_xml from your_table )

SQL Server generating XML with generic field elements

I'm basically trying to reverse what this question is asking...
SQL Server query xml attribute for an element value
I need to produce a result set of "row" elements that contain a group of "field" elements with an attribute that defines the key.
<resultset statement="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="id">1</field>
<field name="version”>0</field>
<field name="property">My Movie</field>
<field name="release_date">2012-01-01</field>
<field name="territory_code”>FR</field>
<field name="territory_description">FRANCE</field>
<field name="currency_code”>EUR</field>
</row>
<row>
<field name="id">2</field>
<field name="version”>0</field>
<field name="property">My Sequel</field>
<field name="release_date">2014-03-01</field>
<field name="territory_code”>UK</field>
<field name="territory_description">United Kingdom</field>
<field name="currency_code”>GBP</field>
</row>
</resultset>
I've got a query that returns this...
<resultset statement="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<id>1</id>
<version>0</version>
<property>My Movie</property>
<release_date>2012-01-01</release_date>
<territory_code>FR</territory_code>
<territory_description>FRANCE</territory_description>
<currency_code>EUR</currency_code>
</row>
<row>
<id>2</id>
<version>0</version>
<property>My Sequel</property>
<release_date>2014-03-01</release_date>
<territory_code>UK</territory_code>
<territory_description>UNITED KINGDOM</territory_description>
<currency_code>GBP</currency_code>
</row>
</resultset>
Using FOR XML PATH ('row'), ROOT ('resultset') in my SQL statement.
What am I missing? Thanks.
It's a bit involved in SQL Server - the normal behavior is what you're seeing - the column names will be used as XML element names.
If you really want all XML elements to be named the same, you'll have to use code something like this:
SELECT
'id' AS 'field/#name',
id AS 'field',
'',
'version' AS 'field/#name',
version AS 'field',
'',
'property' AS 'field/#name',
property AS 'field',
'',
... and so on ....
FROM Person.Person
FOR XML PATH('row'),ROOT('resultset')
This is necessary to make sure the column name is used as the name attribute on the <field> element, and the empty string are necessary so that the SQL XML parser doesn't get confused about which name attribute belongs to what element......
You can do this without having to specify the columns as constants and that will allow you to also use select *. It is a bit more complicated than the answer provided by marc_s and it will be quite a lot slower to execute.
select (
select T.X.value('local-name(.)', 'nvarchar(128)') as '#name',
T.X.value('text()[1]', 'nvarchar(max)') as '*'
from C.X.nodes('/X/*') as T(X)
for xml path('field'), type
)
from (
select (
select T.*
for xml path('X'), type
) as X
from dbo.YourTable as T
) as C
for xml path('row'), root('resultset')
SQL Fiddle
The query creates a derived table where each row has a XML that looks something like this:
<X>
<ID>1</ID>
<Col1>1</Col1>
<Col2>2014-08-21</Col2>
</X>
That XML is then shredded using nodes() and local-name(.) to create the shape you want.
Your SELECT statement needs to look something like this
SELECT
'id' AS [field/#name],
id AS field,
'version' AS [field/#name],
version AS field,
'property' AS [field/#name],
property AS field,
'release_date' AS [field/#name],
release_date AS field,
'territory_code' AS [field/#name],
territory_code AS field,
'territory_description' AS [field/#name],
territory_description AS field,
'currency_code' AS [field/#name],
currency_code AS field

SQL Server 2008 XML Query

I need some more help with another XML query. Below is an example of a record of my XML column:
<Fields>
<MappedFields>
<Field name="FormNumber" value="21" />
<Field name="ProcedureCode" value="T2023" />
<Field name="CurrentDate" value="4/23/2012" />
</MappedFields>
</Fields>
The field elements may appear in any order, so it can appear like this as well:
<Fields>
<MappedFields>
<Field name="ProcedureCode" value="G5532" />
<Field name="FormNumber" value="12" />
<Field name="CurrentDate" value="3/29/2011" />
</MappedFields>
</Fields>
What I am looking for, is a query that will get the value of the Field with the name "FormNumber" for all the records in the table. The query I have below works if the Field with the name of "FormNumber" is the first Field element in the XML. What I need is a query that will find the Field element even if it is not the first element. Can someone help me out with this?
SELECT
X.Node.value(N'(Field/#value)[1]', 'nvarchar(max)') AS FormNumber
FROM
dbo.MHTCM_LetterSent A
CROSS APPLY A.LetterXML.nodes(N'/Fields/MappedFields') AS X(Node)
WHERE
X.Node.value(N'(Field/#name)[1]', 'nvarchar(max)') = 'FormNumber'
You can have a test in the xml path like [#name="FormNumber"].
SELECT
X.Node.value(N'(Field[#name="FormNumber"]/#value)[1]', 'nvarchar(max)') AS FormNumber
FROM
dbo.MHTCM_LetterSent A
CROSS APPLY A.LetterXML.nodes(N'//Fields/MappedFields') AS X(Node)
Note the WHERE isn't needed now.
It might make more sense to move the test to the CROSS APPLY:
SELECT
X.Node.value(N'(./#value)[1]', 'nvarchar(max)') AS FormNumber
FROM
dbo.MHTCM_LetterSent A
CROSS APPLY A.LetterXML.nodes(N'//Fields/MappedFields/Field[#name="FormNumber"]') AS X(Node)
Edit- I made the paths absolute (//) and both examples are working for me.