Extract Date Value from child node - sql

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.

Related

Find the value from XML data in SQL Server?

I want to find the value from XML data in SQL Server table.
Below is my sample xmldata;
<wddxPacket version="1.0">
<header />
<data>
<struct type="xyz">
<var name="TXRGHC43">
<string />
</var>
<var name="TWBS1">
<string>9011750</string>
</var>
<var name="PMNAMEID">
<string>2323443</string>
</var>
<var name="EDATE36">
<string />
</var>
<var name="TWBSDESC40">
<string />
</var>
</struct>
</data>
</wddxPacket>
I am searching for 9011750 under TWBS1. can you please help me on this. how to find 9011750 value.
I am trying the following queries but I didn't get any output.
select
col
from
xyz
where
col.value('(/wddxPacket/data/struct/var[TWBS1])[2]', 'varchar(max)') like '9011750'
SELECT col.value('(/wddxPacket/data/struct/var/string)[3]', 'varchar(100)')
FROM xyz
my requirement is to search the data entire table, that is only sample xml data.
Please try the following SQL. It shows how to use a predicate in the XPath expression to simulate WHERE clause.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (ID INT IDENTITY PRIMARY KEY, xmldata XML);
INSERT INTO #tbl (xmldata)
VALUES
(N'<wddxPacket version="1.0">
<header/>
<data>
<struct type="xyz">
<var name="TXRGHC43">
<string/>
</var>
<var name="TWBS1">
<string>9011750</string>
</var>
<var name="PMNAMEID">
<string>2323443</string>
</var>
<var name="EDATE36">
<string/>
</var>
<var name="TWBSDESC40">
<string/>
</var>
</struct>
</data>
</wddxPacket>');
-- DDL and sample data population, end
DECLARE #varName VARCHAR(20) = 'TWBS1';
SELECT c.value('(./text())[1]','INT') AS [string_value]
FROM #tbl AS tbl
CROSS APPLY tbl.xmldata.nodes('/wddxPacket/data/struct/var[#name=sql:variable("#varName")]/string') AS t(c);
Output
+--------------+
| string_value |
+--------------+
| 9011750 |
+--------------+

Grouping similar records based on one value in xsl 1.0

I need to group the records based on the value in OperationID tag. If more than one record has same operationId, those records should be present in combinations tag and operationId should be common under operationdetails tag. I am using XSL vesion 1.0. Please find below the input xml and the required xml format. Kindly suggest some solution using 1.0
Input xml :
<root>
<records>
<record>
<OperationID>13</OperationID>
<GroupID>00</GroupID>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456789</ID>
<DocumentID>ShowOperationCode20181206071249</DocumentID>
<AllGroupID>JTH</AllGroupID>
<AllID>B21B1</AllID>
</record>
<record>
<OperationID>13</OperationID>
<GroupID>00</GroupID>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456789</ID>
<DocumentID>ShowOperationCode20181206071249</DocumentID>
<AllGroupID>JTT</AllGroupID>
<AllID>B21FB</AllID>
</record>
<record>
<OperationID>14</OperationID>
<GroupID>01</GroupID>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456788</ID>
<DocumentID>ShowOperationCode20181206071250</DocumentID>
<AllGroupID>KTH</AllGroupID>
<AllID>BFFHT</AllID>
</record>
</records>
</root>
required xml format :
<ApplicationArea>
<UTC_TIME>2018-12-06</UTC_TIME>
<ID>123456789</ID>
</ApplicationArea>
<DataArea>
<OpertionsHeader>
<DocumentID>ShowOperationCode20181206071249</DocumentID>
</OpertionsHeader>
<OperationsDetail>
<OperationID>13</OperationID>
<GroupID>00</GroupID>
<Combinations>
<Allowance>
<AllGroupID>JTH</AllGroupID>
<AllID>B21B1</AllID>
</Allowance>
<Allowance>
<AllGroupID>JTT</AllGroupID>
<AllID>B21FB</AllID>
</Allowance>
</Combinations>
</OperationsDetail>
<OperationsDetail>
<OperationID>14</OperationID>
<GroupID>01</GroupID>
<Combinations>
<Allowance>
<AllGroupID>KTH</AllGroupID>
<AllID>BFFHT</AllID>
</Allowance>
</Combinations>
</OperationsDetail>
</DataArea>

XML generate from SQL Query with perfect XML structure

MY SQl "employee" Table Look Like
+-------+---------+--------+----------+
| Empid | Empname | Salary | Location |
+-------+---------+--------+----------+
| 1 | Arul | 100 | Chennai |
+-------+---------+--------+----------+
XML Generate from SQl Query:
select * from employee for xml path, root('root')
from this Sql Query I'm Getting My XML Files as given below
<root>
<employee>
<Empid>1</Empid>
<Empname>Arul</Empname>
<Salary>100</Salary>
<Location>Chennai</Location>
</employee>
</root>
But My Expected Output XML from SQL query as
<root>
<column>Empid</column>
<value>1</value>
<column>Empname</column>
<value>Arul</value>
</root>
As you were told already, the needed output format is really bad and erronous. Nevertheless this can be done quite easily:
DECLARE #mockup TABLE(Empid INT,Empname VARCHAR(100),Salary DECIMAL(10,4),[Location] VARCHAR(100));
INSERT INTO #mockup VALUES(1,'Arul',100,'Chennai')
,(2,'One',200,'More');
SELECT 'Empid' AS [Column]
,EmpId AS [Value]
,'Empname' AS [Column]
,Empname AS [Value]
-- follow this pattern...
FROM #mockup t
FOR XML PATH('employee'),ROOT('root');
The result
<root>
<employee>
<Column>Empid</Column>
<Value>1</Value>
<Column>Empname</Column>
<Value>Arul</Value>
</employee>
<employee>
<Column>Empid</Column>
<Value>2</Value>
<Column>Empname</Column>
<Value>One</Value>
</employee>
</root>
But - by any chance - you should try to change this format. This is awful to query and will be your private headache for sure...
Some better suggestions:
<Employee>
<Column name="EmpId" value="1" />
<Column name="Empname" value="Arul" />
</Employee>
or
<employee id="1" name="Arul" />
or
<employee>
<Id>1</Id>
<Name>Arul</Name>
</employee>
or (if you really, really want to stick with this), at least a column index like here
<root>
<employee>
<Column inx="1">Empid</Column>
<Value inx="1">1</Value>
<Column inx="2">Empname</Column>
<Value inx="2">Arul</Value>
</employee>
<employee>
<Column inx="1">Empid</Column>
<Value inx="1">2</Value>
<Column inx="2">Empname</Column>
<Value inx="2">One</Value>
</employee>
</root>
The query for the last one is this
SELECT 1 AS [Column/#inx]
,'Empid' AS [Column]
,1 AS [Value/#inx]
,EmpId AS [Value]
,2 AS [Column/#inx]
,'Empname' AS [Column]
,2 AS [Value/#inx]
,Empname AS [Value]
-- follow this pattern...
FROM #mockup t
FOR XML PATH('employee'),ROOT('root');

how to update or query xml with xmlns attributes

say my xml doc is this
<root xmlns="http://www.w3.org/2001/XMLSchema-instance">
<parent prop="1">
<child>
<field name="1">
<value1>abc</value1>
<value2>cdf</value2>
</field>
<field name="2">
<value1>efg</value1>
<value2>hjk</value2>
</field>
</child>
</parent>
<parent2>
<prop atrb="2">abc</prop>
</parent2>
</root>
i have it a table newTable2 and xml datatyped column as xmlcol1
here is the query i worte
SELECT xmlcol1.query('/root/parent/child/field/value1/text()') AS a
FROM newTable2
this works when i remove the xmlns attribute if i put it back it does can anyone explain why is it so and how can i query for same keeping the xmlns attribute.
Try this:
;with xmlnamespaces (
default 'http://www.w3.org/2001/XMLSchema-instance'
)
SELECT xmlcol1.query('/root/parent/child/field/value1/text()') AS a_query
, xmlcol1.value('(/root/parent/child/field/value1/text())[1]', 'varchar(255)') AS a_value_1
, xmlcol1.value('(/root/parent/child/field/value1/text())[2]', 'varchar(255)') AS a_value_2
FROM newTable2
never mind i found the answer
i just need to use the
;WITH XMLNAMESPACES(DEFAULT 'http://www.w3.org/2001/XMLSchema-instance')
before the query

Extract data from XML Clob using SQL from db2

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
;